I am trying to create the following example.
<body>
<resources>
<string-array name="mytest">
<item number="1">
<name>Testname</name>
</item>
<item number="2">
<name>blaat..</name>
</item>
</string-array>
</resources>
</body>
I try this by doing the following:
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();
And what i get now is:
<!-- 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>
Can anybody tell me what i’m doing wrong and maybe give me some examples on how it should be done?
Thanks in advance
EDIT: To give a bit more detail: I want to copy some items from test1.xml to test2.xml. So basically the user selects a listitem that points to a number in text1.xml (item number) and that item should then be copied into the (ITEM HERE
Jsoup is generally used for parsing html, not xml, although they have same structure. By default, Jsoup parses anything, then wraps it inside
<html><body>…</body></html>.An example for your goal:
References: