Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java implementation that is similar to C#'s XmlDocument

Tags:

java

c#

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);
like image 681
JME Avatar asked Oct 24 '25 09:10

JME


1 Answers

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.

like image 191
Daniel Kaplan Avatar answered Oct 26 '25 23:10

Daniel Kaplan