Java实现类似于C#的XmlDocument

时间:2013-05-31 20:24:28

标签: c# java

是否存在类似于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);

2 个答案:

答案 0 :(得分:3)

这与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);

这是how to read xml的一个例子:

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

这个问题告诉你如何做xpath:How to read XML using XPath in Java

既然如此,我认为这不是一个非常方便的库。相反,我会调查XStream并尝试找出一种方法来使用它,如果可以的话。这是一个更好的API。

答案 1 :(得分:1)

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