使用nodeList创建XML文档

nath:

我需要使用NodeList创建一个XML Document对象。有人可以帮我做到这一点吗?这是我的Java代码:

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文件:

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

提前致谢。

MarcoS:

您应该这样做:

  • 您创建了一个新org.w3c.dom.Document newXmlDoc的节点,将节点存储在其中NodeList
  • 您创建一个新的根元素,并将其附加到 newXmlDoc
  • 然后,对每个节点n在你的NodeList,你导入nnewXmlDoc,然后追加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);
}

输出为:

<?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,我在此答案中使用了有趣的代码

我希望这有帮助。


如果您不想创建新的根元素,则可以使用原始的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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章