如何使用Java程序动态生成ID为段落添加标记?

时间:2016-03-15 13:42:41

标签: java xml

我有一个xml文件,Xml代码包含一个没有标签名称和标签属性的段落" Id"。我需要动态生成一个新的Id。你可以在里面看到一个p标签里面的两行。但它没有标记名称和Id属性。请帮帮我。示例:我需要的行应该像前两行一样。 代码应为DOM

 <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标记中的文本。我想替换旧文本,我想插入新创建的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>

1 个答案:

答案 0 :(得分:2)

这可能对您有用。我认为以下是您的代码的问题。

  1. 当您执行语句&#34; Id = Id.substring(0,Id.length())+ k;&#34;时,您将替换原始字符串&#34; 271_line _&#34;。< / LI>
  2. 当您添加新的&#34; P&#34;标记,您不能替换现有的文本子元素。
  3. 以下代码可以满足您的期望。

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