类似于c# 's XmlDocument的Java实现

本文关键字:XmlDocument Java 实现 类似于 | 更新日期: 2023-09-27 18:16:23

是否有类似于c#的System.Xml.XmlDocument的java实现?我目前正试图在Java中复制这个c#代码片段。

XmlDocument doc = new XmlDocument();
doc.Load(new XmlTextReader(new StringReader(message)));
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", nameSpace);
XmlNodeList nodeList = doc.SelectNodes("//x:Object//@*", nsmgr);

类似于c# 's XmlDocument的Java实现

这看起来与Java DOM Parser非常相似。下面的代码片段向您展示如何编写xml:

    // Use a Transformer for output
    TransformerFactory tFactory =
    TransformerFactory.newInstance();
    Transformer transformer = 
    tFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    transformer.transform(source, result);

下面是一个如何读取xml的例子:

File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

这个问题告诉你如何使用xpath:如何在Java中使用xpath读取XML

在这种情况下,我不认为这是一个非常方便使用的库。相反,我会研究XStream,如果可以的话,试着找出一种使用它的方法。这是一个更好的API

import java.io.*; import java.util.*; import javax.xml.*;
import javax.xml.namespace.*; import javax.xml.parsers.*;
import javax.xml.xpath.*; import org.w3c.dom.*; import org.xml.sax.*;
public class XPathExample {
    public static void main(String[] args) throws Exception {
        String xml = "<example xmlns:x='"http://example.com/example/'">"
                + "<x:Object attr='"obj1attrvalue'">object 1</x:Object>"
                + "<x:Object attr='"obj2attrvalue'">object 2</x:Object>"
                + "</example>";
        DocumentBuilderFactory docBuilderFactory = 
            DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = 
            docBuilder.parse(new InputSource(new StringReader(xml)));
        Element docEl = doc.getDocumentElement();
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPathBuilder = xPathFactory.newXPath();
        xPathBuilder.setNamespaceContext(new MyNamespaceContext());
        XPathExpression xPath = xPathBuilder.compile("//x:Object//@*");
        NodeList nodeList = 
            (NodeList) xPath.evaluate(docEl, XPathConstants.NODESET);
        System.out.println("nodeList length=" + nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            System.out.println("nodeList[" + i + "]=" + node);
        }
    }
    private static final class MyNamespaceContext implements NamespaceContext {
        public String getNamespaceURI(String prefix) {
            if ("x".equals(prefix)) {
                return "http://example.com/example/";
            }
            return null;
        }
        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }
        public String getPrefix(String namespaceURI) {
            return null;
        }
    }
}