July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with XML and Java

  • January 2, 2008
  • By Rob Lybarger
  • Send Email »
  • More Articles »

Example Revisited

Revisiting the demo.xml file from above, consider the following example that parses the file into a DOM object and then uses some XPath evaluations to display some information:

DocumentBuilderFactory dbFactory =
   DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbFactory.newDocumentBuilder();
Document doc = builder.parse(new File("demo.xml"));

XPathFactory xpFactory = XPathFactory.newInstance();
XPath xpath = xpFactory.newXPath();

int count = Integer.parseInt(xpath.evaluate("count(/demo/child)",
                             doc));
System.out.println("There are "+count+" child elements:");

for (int i=1 ; i<=count ; i++) {
  String text = xpath.evaluate("/demo/child["+i+"]", doc);
  System.out.printf("i=%d child text is '%s'\n", i, text);
}

One interesting point to note is that you are no longer having to deal explicitly with getting a NodeList object just to call the getLength() method—the XPath language provides a built-in count() function that accomplishes the same objective. (In fact, in complex XPath statements, the value of a count() function can be used as a constraint predicate inside a larger XPath expression to, for example, get nodes with no children.) Also, this example has no references to Node or Element objects, because you are using the form of the evaluate method that directly returns Strings. Of course, in a more real-world scenario, you are likely to mix and match approaches somewhat, depending on your exact needs.

Conclusion

You have just had a quick tour of some Java's built-in XML processing capabilities. Although the programming style of working with XML does in some respects differ from that of the rest of the API, the upshot of learning this approach is that it is at least "mentally portable" to many other languages. (Even JavaScript is functionally similar, should you ever dabble in AJAX-style response processing.) Of course, those with a real need to do intensive XML munging should probably at least look in on the many third-party libraries, but you should not be afraid to take things on yourself if requirements so demand.

Other Ideas

A great way to utilize XPath expressions to streamline, say, reading an XML configuration file for your application would be to store them into a standard properties file. Then, you simply can fetch an appropriate XPath expression from this file based on some a appropriately named "key" value, and evaluate it against the configuration file DOM already loaded. This allows you some flexibility to rearrange the structure of the configuration file (if needed) without having to rearrange any existing source code. With the right helper files in place, large sections of your own code won't even know that XML is involved.





Page 5 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel