Reputation: 2353
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);
Upvotes: 4
Views: 3922
Reputation: 10136
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;
}
}
}
Upvotes: 1
Reputation: 67300
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.
Upvotes: 4