简体   繁体   English

如何使用jsoup文档将子项添加到子节点

[英]How to add children to childnodes 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 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. 编辑:提供更多细节:我想将一些项目从test1.xml复制到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 所以基本上用户选择一个指向text1.xml(项目编号)中的数字的listitem,然后该项目应该被复制到(ITEM HERE)

Jsoup is generally used for parsing html, not xml, although they have same structure. Jsoup通常用于解析html,而不是xml,尽管它们具有相同的结构。 By default, Jsoup parses anything, then wraps it inside <html><body> ... </body></html> . 默认情况下,Jsoup解析任何内容,然后将其包装在<html><body> ... </body></html>

An example for your goal: 您的目标的一个例子:

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");
}

References: 参考文献:

This doesn't solve your exact problem, but you should be able to figure it out from here. 这并不能解决您的确切问题,但您应该能够从这里解决。 I do pretty much create test2.xml as a new document. 我几乎将test2.xml创建为一个新文档。 So, if that exists with information in it, you will have to work around that. 因此,如果存在信息,那么您将不得不解决这个问题。

    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 output: 这是输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM