使用JDOM将文本解析为XML

问题描述:

我是一名新手,需要您的帮助。使用JDOM将文本解析为XML

我转换文本到XML(有一些额外的属性)

我的文字是 样条(N,-1.151,1.002,-0.161,0.997,0.840,-0.004,概述)

我必须使用XML等JDOM以下

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

我可以简单地转换如果N是固定的,在上面的例子中N = 3,所以因此具有3×3个y坐标。但是,如果我使用for循环如下所示,结果不如例外。任何帮助将是巨大的

 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); 

从上面的代码

你不应该建立在循环内花键元件,它应该是外面......(和变量名应该是小写)

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]);