使用nodeList创建XML文档

我需要使用NodeList创建一个XML Document对象。 有人可以帮我做这件事。 我已经向您展示了下面的代码和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文件如下所示

    Real   play    

提前致谢..

你应该这样做:

  • 你创建一个新的org.w3c.dom.Document newXmlDoc ,你在NodeList存储节点,
  • 您创建一个新的根元素,并将其附加到newXmlDoc
  • 然后,对于NodeList每个节点n ,在newXmlDoc导入n ,然后将n附加为root的子节点

这是代码:

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

输出是:

   Real  play  

一些说明:

  • 我已将exp改为/configs/markets/market ,因为我怀疑你想要复制market元素,而不是单一的markets元素
  • 对于printXmlDocument ,我在这个答案中使用了有趣的代码

我希望这有帮助。


如果您不想创建新的根元素,那么您可以使用原始的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(); } } 

这将为您提供以下输出:

    Real   play   

你可以尝试Document的adoptNode()方法。

也许你需要迭代你的NodeList 。 您可以使用nodeList.item(i)访问各个Nodes

如果要将搜索结果包装在Element ,可以使用Document createElement()和新创建的Element上的appendChild()