...">

How to add children to children using jsoup document

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 now I get the following:

 <!-- 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 someone tell me what I'm doing wrong, and maybe give some examples of how to do this?

Thanks in advance

EDIT: To get a little more detailed: I want to copy some elements from test1.xml to test2.xml. So basically the user selects a listitem that points to the number in text1.xml (position number), and this element should then be copied to (ITEM HERE

+4
source share
2 answers

Jsoup is usually used to parse html, not xml, although they have the same structure. By default, Jsoup parses something, then completes it inside <html><body> ... </body></html> .

An example for your purpose:

 import org.jsoup.nodes.*; Document doc = Jsoup.parse(""); // clear <html><body></body></html> doc.html(""); Element e = doc.appendElement("body").appendElement("resources"); e = e.appendElement("string-array"); e.attr("name", "mytest"); for (int i = 0; i < 10; i++) { Element sub = e.appendElement("item"); sub.attr("number", Integer.toString(i)); sub.appendElement("name").text("blahh"); } 

Literature:

+8
source

This will not solve your exact problem, but you must understand it. I pretty much create test2.xml as a new document. So, if it exists with information in it, you will have to get around this.

  String html = "<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>"; Document test1 = Jsoup.parse(html); Document test2 = Jsoup.parse(""); test2.body().append("<resources>"); test2.select("resources").append("<string-array name='mytest'>"); test2.select("[name=mytest]").append(test1.select("item[number=1]").toString()); test2.select("[name=mytest]").append(test1.select("item[number=2]").toString()); System.out.println(test2.body().children()); 

Here is the result:

  <resources> <string-array name="mytest"> <item number="1"> <name> Testname </name> </item> <item number="2"> <name> blaat.. </name> </item> </string-array> </resources> 
+1
source

Source: https://habr.com/ru/post/1399182/


All Articles