0

I an newbee need you your help.

I am converting a text in to XML (with some extra attributes)

my text is SPLINE(N, -1.151,1.002, -0.161,0.997, 0.840,-0.004, OUTLINED)

i need to have in XML using JDOM like following

 <SPLINE n="3" x1="0.840" y1="-1.004" x2 ="-0.161" y2 ="0.997"  x3 ="0.840" y3"-0.004"  prim_style="OUTLINED" />

I can convert simply if N is fixed , in the above example N= 3 , so therefore has 3 x and 3 y coordinates. But If I am using for loop as below the result is not as excepted . any help will be great

      root.addContent(child);
                document.setContent(root);


                            int i = Integer.parseInt(temp_token[2]);
                            int count = i * 2 + i + 4;

                            for (int j = 0; j <= count - 5; j = j + 3) {

                                String x = null;
                                String y = null;

                                Element SPLINE = new Element("SPLINE")
                                        .setAttribute("n", temp_token[2])
                                        .setAttribute("x", temp_token[j + 4])
                                        .setAttribute("y", temp_token[j + 5])
                                        .setAttribute("prim_style",
                                                temp_token[count]);


child.addContent(SPLINE);

From the above code

Ganderous
  • 11
  • 4

1 Answers1

0

You should not be creating the SPLINE element inside the loop, it should be outside... (and variable names should be lowercase)

root.addContent(child);
document.setContent(root);


int i = Integer.parseInt(temp_token[2]);
int count = i * 2 + i + 4;

Element spline = new Element("SPLINE");
child.addContent(SPLINE);
spline.setAttribute("n", temp_token[2]);

int pair = 0;
for (int j = 0; j <= count - 5; j = j + 3) {
    pair++;
    spline.setAttribute("x" + pair, temp_token[j + 4]);
    spline.setAttribute("y" + pair, temp_token[j + 5]);
}
spline.setAttribute("prim_style",temp_token[count]);
rolfl
  • 17,539
  • 7
  • 42
  • 76