2016-03-15 70 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)标签。 –