2012-07-10 92 views
0

我是一名新手,需要您的幫助。使用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); 

從上面的代碼

回答

0

你不應該建立在循環內花鍵元件,它應該是外面......(和變量名應該是小寫)

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