If you need to parse an XML file then the XPath API can save boatloads of time. This isn't a Java specific API, but that's where I use it.
Links:
Super simple way to get a single value from an XML doc:
{CODE()}
// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.Document document = builder.parse(new File("/widgets.xml"));
// evaluate the XPath expression against the Document
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget[@name='a'?]/@quantity";
Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER);
{/CODE()}
The point there is that you can access any element or attribute directly just by modifying the expression variable above. In that example, "/happy/smiles@smileWidth" will get the value "32" from the following xml doc:
<happy>
<smiles smileWidth="32"/>
</happy>
The following returns the text content of the <c> element.
"/a/b/c/text()"
The following gives you the 2nd and 3rd characters of the text of the <c> element (I know something like this works. I didn't actually test this though.).
"substring(/a/b/c/text(),2,3)"
If you don't start it with a "/" then the path will be relative to the Element you are using it on instead of to the root.
Links:
- Java API
- http://www.w3.org/TR/xpath?
- w3schools quick intro
- XPath Explorer This is a very handy utility that makes it every easy to figure out how to use xpaths. It even has an eclipse plugin, but I just use the standalone app.
Super simple way to get a single value from an XML doc:
{CODE()}
// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.Document document = builder.parse(new File("/widgets.xml"));
// evaluate the XPath expression against the Document
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget[@name='a'?]/@quantity";
Double quantity = (Double) xpath.evaluate(expression, document, XPathConstants.NUMBER);
{/CODE()}
The point there is that you can access any element or attribute directly just by modifying the expression variable above. In that example, "/happy/smiles@smileWidth" will get the value "32" from the following xml doc:
<happy>
<smiles smileWidth="32"/>
</happy>
The following returns the text content of the <c> element.
"/a/b/c/text()"
The following gives you the 2nd and 3rd characters of the text of the <c> element (I know something like this works. I didn't actually test this though.).
"substring(/a/b/c/text(),2,3)"
If you don't start it with a "/" then the path will be relative to the Element you are using it on instead of to the root.
Version 5.1 last modified by Geoff Fortytwo on 11/07/2010 at 19:17
Document data
Attachments:
No attachments for this document