0

私はあなたの助けが必要です。

テキストを XML に変換しています (いくつかの追加属性を使用)

私のテキストは SPLINE(N, -1.151,1.002, -0.161,0.997, 0.840,-0.004, OUTLINED) です

次のようにJDOMを使用してXMLにする必要があります

 <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 つの x 座標と 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);

上記のコードから

4

1 に答える 1

0

ループの内側に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]);
于 2012-07-10T11:28:45.497 に答える