繁体   English   中英

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

[英]How to add children to childnodes using jsoup document

我正在尝试创建以下示例。

<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>

任何人都可以告诉我我做错了什么,也许可以给我一些如何做的例子?

提前致谢

编辑:提供更多细节:我想将一些项目从test1.xml复制到test2.xml。 所以基本上用户选择一个指向text1.xml(项目编号)中的数字的listitem,然后该项目应该被复制到(ITEM HERE)

Jsoup通常用于解析html,而不是xml,尽管它们具有相同的结构。 默认情况下,Jsoup解析任何内容,然后将其包装在<html><body> ... </body></html>

您的目标的一个例子:

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

参考文献:

这并不能解决您的确切问题,但您应该能够从这里解决。 我几乎将test2.xml创建为一个新文档。 因此,如果存在信息,那么您将不得不解决这个问题。

    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());

这是输出:

    <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