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.
Version 5.1 last modified by Geoff Fortytwo on 11/07/2010 at 19:17

Attachments 0

No attachments for this document
Website Top
Send Me Mail!:
   g42website4 AT g42.org
My Encyclopaedia Blog

Creator: Geoff Fortytwo on 2008/05/12 01:16
Copyright 2004-2007 (c) XPertNet and Contributing Authors
1.3.2.9174