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.


Super simple way to get a single value from an XML doc:
// 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);

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:
<smiles smileWidth="32"/>

The following returns the text content of the <c> element.

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.).

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