JAXP/XPath example - II

28 June 2008

Continued from the last post. I will show how to parse a document using JAXP.

I will present an example that will search a XML file and then prints out the titles of all the books it finds.
First step is to load the document into a DOM Document object. I will use books.xml file in the example and it is assumed that it resides in the current directory:

Here’s a simple code fragment that parses the document and constructs the corresponding Document object:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

continued …

del.icio.us:JAXP/XPath example - II  digg:JAXP/XPath example - II  spurl:JAXP/XPath example - II  wists:JAXP/XPath example - II  simpy:JAXP/XPath example - II  newsvine:JAXP/XPath example - II  blinklist:JAXP/XPath example - II  furl:JAXP/XPath example - II  reddit:JAXP/XPath example - II  fark:JAXP/XPath example - II  blogmarks:JAXP/XPath example - II  Y!:JAXP/XPath example - II  smarking:JAXP/XPath example - II  magnolia:JAXP/XPath example - II  segnalo:JAXP/XPath example - II  gifttagging:JAXP/XPath example - II

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: JAXP/XPath example - II

Leave a Reply