3

是否有类似于 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);
4

2 回答 2

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

这是一个如何读取 xml的示例:

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

这个 SO 问题向您展示了如何执行 xpath:How to read XML using XPath in Java

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

于 2013-05-31T20:29:55.393 回答
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;
        }
    }
}
于 2013-05-31T22:59:09.007 回答