2016-03-15 65 views
2

我有一個xml文件,Xml代碼包含一個沒有標籤名稱和標籤屬性「Id」的段落。我需要動態生成一個新的Id。你可以在裏面看到一個p標籤裏面的兩行。但它沒有標籤名稱和Id屬性。請幫助我。例如:我需要的行應該像前兩行一樣。 代碼應該是DOM如何使用Java程序動態生成ID爲段落添加標籤?

<p id ="271_line_1">My boy are play cricket </p> 
<p id ="271_line_2">to win the game. Where they are playing? </p> 

<ExampleProgram> 
<catalog> 
<book id="bk101"> 
    <author>Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <description>An in-depth look at creating applications 
    with XML.</description> 
    </book> 
    <book id="bk102"> 
    <author>Gambardella, Matthew</author> 
    <p class="paragraph " id="271__paragraph_8"> 
    My boy are play cricket 
    <iline class="iline_list " id="271_iline_list_9" response="SAMPLE"> 
    <iline class="option " id="271_option_15" prefer="i1">SCHOOL</iline> 
    <iline class="option " id="271_option_18" prefer="i2">GROUND</iline> 
    <iline class="option " id="271_option_19" prefer="i4">HOME</iline> 
    </iline> 
    to win the game. Where they are playing? 
    </p> 
    </book> 
    <book id="bk103"> 
    <author>Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-12-16</publish_date> 
    <description>A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</description> 
    </book> 
    </catalog> 
    </ExampleProgram> 

方法變化p標籤:現在我可以能夠創造一個p標籤,我可以能夠手動設置屬性ID。但新的p標籤並未替換父標籤中的文本。我想替換舊文本,我想插入新創建的p標籤。我想創建動態ID而不是手動ID。

private static String createPTagId(Node someNode) { 
String Id ="271_line_" 
    NodeList childs = someNode.getChildNodes(); 
    for (int i = 0; i < childs.getLength();i++) { 
     Node child = childs.item(i); 
     if (child.getNodeType() == Document.ELEMENT_NODE) { 
      if (child.getNodeName().equalsIgnoreCase("p")) { 

        Value = child.getAttributes().getNamedItem("id").getNodeValue(); 
        System.out.println("Attribute Value :" + Value); 
        printElement(doc); 
       } 
      } else { 
       createPTagId(child); 
       } 
     } 
     if(child.getNodeName().equals("p") && child.getAttributes().getNamedItem("id").getNodeValue().contains(Value)){ 

      Node nextNode = someNode.getFirstChild(); 
      System.out.println("1 "+child.getFirstChild().getNodeValue()); 
      System.out.println("2 "+child.getLastChild().getNodeValue()); 

      String strP= child.getFirstChild().getNodeValue().toString().trim(); 
      String strP1= child.getLastChild().getNodeValue().toString().trim(); 
      int k ; 
      for(k=1;k<3;k++){ 
        Id = Id.substring(0,Id.length())+k; 
        System.out.println(Id); 
      } 
      Element title = doc.createElement("P"); 
      title.appendChild(doc.createTextNode(strP)); 
      title.setAttribute("id",Id); 
      nextNode.appendChild(title); 
      Node nextNodes = someNode.getLastChild(); 
      Element title1 = doc.createElement("P"); 
      title1.appendChild(doc.createTextNode(strP1)); 
      title1.setAttribute("id",Id); 
      nextNodes.appendChild(title1); 

     } 

     } 

錯誤輸出:

<catalog> 
<book id="bk101"> 
    <author>Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <price>44.95</price> 
    <publish_date>2000-10-01</publish_date> 
    <description>An in-depth look at creating applications 
    with XML.</description> 
</book> 
<p class="paragraph " id="271__paragraph_8"> 
My boy is play cricket 
<iline class="iline_list " id="271_iline_list_9" response="SAMPLE"> 
<iline class="option " id="271_option_15" prefer="i1">wind</iline> 
<iline class="option " id="271_option_18" prefer="i2">cloth</iline> 
<iline class="option " id="271_option_19" prefer="i4">ribbon</iline> 
</iline> 
to win the game. Where they are playing? 
---<p id ="271_line_">My boy are play cricket </p>--- 
---<p id ="271_line_">to win the game. Where they are playing? </p> 
</p> 
<book id="bk102"> 
    <author>Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <price>5.95</price> 
    <publish_date>2000-12-16</publish_date> 
    <description>A former architect battles corporate zombies, 
    an evil sorceress, and her own childhood to become queen 
    of the world.</description> 
</book> 
</catalog> 

我可以創建p標籤,但它沒有在同一個地方,我得到它再次放置。它被放置爲父母的最後一個孩子P標記。我可以爲創建的新P標籤動態創建id屬性。

新的XML:

<Liberary> 
<booksInLiberary 
class="book_list_1" id="271_book_list_9" response="bookInLiberary"> 
<p class="paragraph " id="271__paragraph_8"> 
The Science subject score got increase 
<iline class="iline_list " id="271_iline_list_9" response="SAMPLE"> 
<iline class="option " id="271_option_15" prefer="i1">wind</iline> 
<iline class="option " id="271_option_18" prefer="i2">cloth</iline> 
<iline class="option " id="271_option_19" prefer="i4">ribbon</iline> 
</iline> 
,buy coming Exam. Pass percentage 35% increase? 
</p> 
</bookInLiberary> 
<booksWithStudents 
class="book_list_2" id="271_book_list_9" response="bookWithStudents"> 
<p class="paragraph " id="271__paragraph_8"> 
The Duke unvirsity students renew 
<iline class="iline_list " id="271_iline_list_9" response="SAMPLE"> 
<iline class="option " id="271_option_15" prefer="i1">wind</iline> 
<iline class="option " id="271_option_18" prefer="i2">cloth</iline> 
<iline class="option " id="271_option_19" prefer="i4">ribbon</iline> 
</iline> 
the books to study for Exams 
</p> 
</booksWithStudents> 
<Liberary> 

回答

2

這可能是對你有用。我認爲以下是你的代碼的問題。

  1. 當您執行語句「Id = Id.substring(0,Id.length())+ k;」時,您將替換原始字符串「271_line_」。
  2. 當您添加新的「P」標記時,您不會替換現有的文本子元素。

下面的代碼將做你期望的。

NodeList pnodes = doc.getElementsByTagName("p"); 
     for(int i=0;i<pnodes.getLength();i++){ 
      NodeList nodes = pnodes.item(i).getChildNodes(); 
      int k = 0; 
      for(int j = 0;j<nodes.getLength();j++){ 
       if(nodes.item(j).getNodeName().equals("#text")){////Check if the current child element is a text node or not, if yes then replace it with the new P element 
        k++; 
        Element title = doc.createElement("P"); 
        title.appendChild(doc.createTextNode(nodes.item(j).getNodeValue())); 
        String tempId = Id + k; // generate the new Id 
        title.setAttribute("id",tempId); 
        pnodes.item(i).replaceChild(title, nodes.item(j)); // replace the child element, instead of appending 
       } 
      } 
+0

現在您直接使用getElementByTagName()獲取了一個p標記。但例如,如果我在其他Body3標籤中有一個P標籤,這將是問題。我需要採用父標籤Body2而不是P標籤,並檢查哪個子小孩單獨有一個文本,並且沒有標籤名稱或僅有文本。 –

+0

如果我正確理解你的擔心,那麼還有一些其他標籤必須替換簡單文本元素的子節點。爲此,您可以執行與之前對「P」元素所做的相同處理。 – greenPadawan

+0

查看頂部我給新的xml:在該XML中,我有父標籤(Liberary)裏面有2個子標籤(booksInLiberary)和(booksWithStudents) 我需要創建一個名稱爲(p)標籤名稱,它位於子標籤(booksInLiberary)內。如果我使用getElementbyTagName()取得(P)標籤 它會影響其他沒有標籤的空白文本。我只需要爲bookInLiberary子標籤中的文本創建(p)標籤。 –