java – 使用nodeList创建XML文档

前端之家收集整理的这篇文章主要介绍了java – 使用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文件如下所示

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

提前致谢..

解决方法

你应该这样做:

>您创建一个新的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);
}

输出是:

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

一些说明:

>我已经将exp改为/ configs / markets / market,因为我怀疑你想要复制市场元素,而不是单一的市场元素
>对于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>

猜你在找的Java相关文章