次の例を作成しようとしています。jsoup文書を使用して子ノードに子を追加する方法
<body>
<resources>
<string-array name="mytest">
<item number="1">
<name>Testname</name>
</item>
<item number="2">
<name>blaat..</name>
</item>
</string-array>
</resources>
</body>
私は、次の手順を実行して、これを試してみてください。
FileInputStream fis = openFileInput("test1.xml");
Document doc = Jsoup.parse(fis, "UTF-8", "");
Node node = doc.getElementsByTag("item").get(getPosition());
fis.close();
fis = openFileInput("test2.xml");
Document doc2 = Jsoup.parse(fis, "UTF-8", "");
fis.close();
Elements test = doc2.getElementsByTag("resources");
if(test.size() < 0){
fis = openFileInput("test2.xml");
doc2 = Jsoup.parse(fis, "UTF-8", "");
fis.close();
doc2.appendElement("resources").parent();
FileOutputStream os = openFileOutput("test2.xml", Context.MODE_PRIVATE);
os.write(doc2.toString().getBytes());
os.close();
fis = openFileInput("test2.xml");
doc2 = Jsoup.parse(fis, "UTF-8", "");
fis.close();
doc2.appendChild(doc2.appendElement("string-array").attr("name", "mytest")).parent();
os = openFileOutput("test2.xml", Context.MODE_PRIVATE);
os.write(doc2.toString().getBytes());
os.close();
System.out.println("Created file\n");
}
doc2.appendChild(node);
FileOutputStream os = openFileOutput("test2.xml", Context.MODE_PRIVATE);
os.write(doc2.toString().getBytes());
os.close();
そして、何私は今を取得することです:
<!-- test1.xml (input) -->
<resources>
<string-array name="firsttest">
<item number="1">
<name>Testname</name>
</item>
<item number="2">
<name>blaat..</name>
</item>
<item number="3">
<name>Next item</name>
</item>
</string-array>
</resources>
<!-- test2.xml (output)-->
<body>
<resources></resources>
<string-array name="mytest"></string-array>
<item number="1">
<name>Testname</name>
</item>
<item number="2">
<name>blaat..</name>
</item>
</body>
誰もが私がやっているものを私に教えてもらえます間違っているかもしれないし、多分それをどうやってやるべきかの例をいくつか教えてください事前に
おかげ
EDIT:もう少し詳細を与える:私はtest2.xmlにtest1.xmlからいくつかのアイテムをコピーします。したがって、基本的にユーザーはtext1.xml(アイテム番号)内の数字を指し示すリストアイテムを選択し、そのアイテムをコピーする必要があります(項目は
まず、問題のないものを削除し、変数をより自己文書化する方法でコードをクリーンアップします。 (例えば、 'if(test.size()<0)'は何もしません。なぜなら、サイズは0より小さくないからです) – cdeszaq
私は自分のコードでそれを掃除しました。自分自身の問題。しかし、それらのコメントはあまり有用ではありません私は恐れています。 – stackr
test2.xmlの内容を投稿できますか?また、jsoupはHTML解析とXML解析に適していますが、これを動作させることができます。 –