2010-07-14 146 views
12

這裏某些元素之前,如何插入元素插入XML /後是我的代碼,也許你會馬上什麼我缺少發現:在Java

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = domFactory.newDocumentBuilder(); 
Document doc = builder.parse(fileName)); 

XPathFactory factory = XPathFactory.newInstance(); 
XPath xpath = factory.newXPath(); 
XPathExpression expr = xpath.compile("//CustomerId"); 

Object result = expr.evaluate(doc, XPathConstants.NODESET); 
Nodelist nodes = (NodeList) result; 
Text a = doc.createTextNode("value"); 
Element p = doc.createElement("newNode"); 
p.appendChild(a); 

for (int i = 0; i < nodes.getLength(); i++) { 
    nodes.item(i).insertBefore(p, nodes.item(i)); 
} 

我試圖插入新節點( CustomerId現有節點之前)。下面是我的XML樣本文件:

<Customer> 
    <names> 
     <firstName>fName</firstName> 
     <lastName>lName</lastName> 
     <middleName>nName</middleName> 
     <nickName/> 
    </names> 
    <addressList> 
     <address> 
      <streetInfo> 
       <houseNumber>22</houseNumber> 
       <baseName>Street base name</baseName> 
       <district>kewl district</district> 
      </streetInfo> 
      <zipcode>22231</zipcode> 
      <state>xxx</state> 
      <country>xxxz</country> 
      <primary>true</primary> 
     </address> 
    </addressList> 
    <CustomerId/> 
    <SSN>561381</SSN> 
    <phone> 
     <homePhone>123123123</homePhone> 
     <officePhone/> 
     <homePhone>21319414</homePhone> 
    </phone> 
    <preferred>true</preferred> 
</Customer> 

這是越來越拋出我不知道還有什麼嘗試一個例外:

NOT_FOUND_ERR:試圖以 參考的一個節點發上下文 不存在。

回答

27

在這裏,我只是用你提供的XML樣本測試的例子。

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
domFactory.setIgnoringComments(true); 
DocumentBuilder builder = domFactory.newDocumentBuilder(); 
Document doc = builder.parse(new File("XmlTest.xml")); 

NodeList nodes = doc.getElementsByTagName("CustomerId"); 

Text a = doc.createTextNode("value"); 
Element p = doc.createElement("newNode"); 
p.appendChild(a); 

nodes.item(0).getParentNode().insertBefore(p, nodes.item(0)); 

下面是結果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<Customer> 
    <names> 
     <firstName>fName</firstName> 
     <lastName>lName</lastName> 
     <middleName>nName</middleName> 
     <nickName/> 
     </names> 
    <addressList> 
     <address> 
      <streetInfo> 
       <houseNumber>22</houseNumber> 
       <baseName>Street base name</baseName> 
       <district>kewl district</district> 
       </streetInfo> 
      <zipcode>22231</zipcode> 
      <state>xxx</state> 
      <country>xxxz</country> 
      <primary>true</primary> 
      </address> 
     </addressList> 
    <newNode>value</newNode> 
<CustomerId/> 
    <SSN>561381</SSN> 
    <phone> 
     <homePhone>123123123</homePhone> 
     <officePhone/> 
     <homePhone>21319414</homePhone> 
     </phone> 
    <preferred>true</preferred> 
</Customer> 

如果你有興趣,這裏是我用來顯示結果的示例代碼:

Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 

StreamResult result = new StreamResult(new StringWriter()); 
DOMSource source = new DOMSource(doc); 
transformer.transform(source, result); 

String xmlOutput = result.getWriter().toString(); 
System.out.println(xmlOutput); 
+0

幫助... .. !!! – 2015-04-28 06:27:58

5

我想你想插入到父母,而不是孩子:

nodes.item(i).getParentNode().insertBefore(p, nodes.item(i));