2011-04-26 85 views
6

我需要使用NodeList創建XML文檔對象。有人能幫我做到這一點。我已經向您展示的代碼及以下使用nodeList創建XML文檔

import 
javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.*; import 
org.w3c.dom.*; 

public class ReadFile { 

    public static void main(String[] args) { 
     String exp = "/configs/markets"; 
     String path = "testConfig.xml"; 
     try { 
      Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(path); 
      XPath xPath = XPathFactory.newInstance().newXPath(); 
      XPathExpression xPathExpression = xPath.compile(exp); 
      NodeList nodes = (NodeList) 
       xPathExpression.evaluate(xmlDocument, 
             XPathConstants.NODESET); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

XML文件的XML如下所示

<configs> 
    <markets> 
     <market> 
      <name>Real</name> 
     </market> 
     <market> 
      <name>play</name> 
     </market> 
    </markets> 
</configs> 

在此先感謝..

回答

12

你應該做的是這樣的:

  • 創建新org.w3c.dom.Document newXmlDoc您存儲節點在NodeList
  • 您創建一個新的根元素,將其追加到newXmlDoc
  • 然後,對每個節點nNodeList,你導入newXmlDocn,然後追加nroot

這孩子是代碼:

public static void main(String[] args) { 
    String exp = "/configs/markets/market"; 
    String path = "src/a/testConfig.xml"; 
    try { 
     Document xmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().parse(path); 

     XPath xPath = XPathFactory.newInstance().newXPath(); 
     XPathExpression xPathExpression = xPath.compile(exp); 
     NodeList nodes = (NodeList) xPathExpression. 
       evaluate(xmlDocument, XPathConstants.NODESET); 

     Document newXmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().newDocument(); 
     Element root = newXmlDocument.createElement("root"); 
     newXmlDocument.appendChild(root); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Node node = nodes.item(i); 
      Node copyNode = newXmlDocument.importNode(node, true); 
      root.appendChild(copyNode); 
     } 

     printTree(newXmlDocument); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

public static void printXmlDocument(Document document) { 
    DOMImplementationLS domImplementationLS = 
     (DOMImplementationLS) document.getImplementation(); 
    LSSerializer lsSerializer = 
     domImplementationLS.createLSSerializer(); 
    String string = lsSerializer.writeToString(document); 
    System.out.println(string); 
} 

輸出是:

<?xml version="1.0" encoding="UTF-16"?> 
<root><market> 
      <name>Real</name> 
     </market><market> 
      <name>play</name> 
     </market></root> 

一些注意事項:

  • 我已經改變了exp/configs/markets/market,因爲我懷疑你要複製的market元素,而不是單一的markets元素
  • printXmlDocument,我使用了有趣的代碼在此answer

我希望這有幫助。


如果你不希望創建一個新的根元素,那麼你可以使用你原來的XPath表達式,它返回一個NodeList由單個節點(請記住,你的XML必須有一個根元素),您可以直接將其添加到新的XML文檔中。

請參見下面的代碼,在那裏我評論從上面的代碼行:

public static void main(String[] args) { 
    //String exp = "/configs/markets/market/"; 
    String exp = "/configs/markets"; 
    String path = "src/a/testConfig.xml"; 
    try { 
     Document xmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().parse(path); 

     XPath xPath = XPathFactory.newInstance().newXPath(); 
     XPathExpression xPathExpression = xPath.compile(exp); 
     NodeList nodes = (NodeList) xPathExpression. 
     evaluate(xmlDocument,XPathConstants.NODESET); 

     Document newXmlDocument = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder().newDocument(); 
     //Element root = newXmlDocument.createElement("root"); 
     //newXmlDocument.appendChild(root); 
     for (int i = 0; i < nodes.getLength(); i++) { 
      Node node = nodes.item(i); 
      Node copyNode = newXmlDocument.importNode(node, true); 
      newXmlDocument.appendChild(copyNode); 
      //root.appendChild(copyNode); 
     } 

     printXmlDocument(newXmlDocument); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

這會給你以下的輸出:

<?xml version="1.0" encoding="UTF-16"?> 
<markets> 
     <market> 
      <name>Real</name> 
     </market> 
     <market> 
      <name>play</name> 
     </market> 
    </markets> 
+0

它工作正常馬爾科。但問題是有一個叫做root的元素,它並不在xml文檔中。有沒有辦法做到這一點,沒有根元素。在此先感謝 – nath 2011-04-26 08:40:21

+0

,您需要一個XML中的根元素。你可能要做的是用你原來的XPath('String exp =「/ configs/markets」;')提取'markets',然後你的'NodeList'將包含一個節點,它可以直接導入並附加到你的新XML文件:見編輯答案。 – MarcoS 2011-04-26 08:52:44

+0

感謝Marco。現在它的工作很好.. :) – nath 2011-04-26 09:37:28

0

您可以嘗試DocumentadoptNode()方法。

也許你需要遍歷你的NodeList。您可以通過nodeList.item(i)訪問個人Nodes

如果你想包裝在Element搜索結果,你可以使用的createElement()的新創建的DocumentappendChild()Element