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