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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With