3

Is there a java implementation that is similar to C#'s System.Xml.XmlDocument? I am currently trying to replicate this C# code fragment in Java.

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);
JME
  • 2,293
  • 9
  • 36
  • 56

2 Answers2

3

This looks very similar to the Java DOM Parser. Here's a snippet to show you how to write 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);

And here's an example of 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);

And this SO question shows you how to do xpath: How to read XML using XPath in Java

That being the case, I don't think this is a very convenient library to use. Instead I would look into XStream and try to figure out a way to use it if you can. It's a much better API.

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • I only need this code because I need a way to search through all the nodes for a specific reference and convert that reference's datetime to standard UTC. Otherwise I am using a different library to serialize and deserialize xml messages. – JME May 31 '13 at 20:45
  • Ah, I thought you were trying to write, not read. Let me edit – Daniel Kaplan May 31 '13 at 20:47
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;
        }
    }
}
Mike Clark
  • 10,027
  • 3
  • 40
  • 54