ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Storing an XML Document in Apache Xindice
Pages: 1, 2, 3, 4, 5, 6

Retrieving an XML Document from the Database

Next, retrieve the XML document that was added in the previous section. Retrieve an XML document from the Xindice database with the command:



>xindice rd -c xmldb:xindice://localhost:7001/db/catalog  
  -n catalog.xml

This retrieves the XML document. Next, retrieve the XML document with the XML:DB API. Obtain the catalog collection as in the "Adding an XML Document to the Database" section. Obtain the XML resource for the XML document. Output the XML document in the XML resource.

XMLResource resource = (XMLResource) 
  (collection.getResource(resourceID)); 
System.out.println(resource.getContent());

This retrieves the XML document added in the previous section.

Querying the Database with XPath

Xindice provides the XPath Query engine to query the XML document in the database with XPath. Next, query the XML document in the Xindice database with XPath. First, with the command-line tool, query the Xindice database collection with the xindice command xpath action. For example, retrieve the title of the <article/> in the first <journal/> element.

>xindice xpath -c xmldb:xindice://localhost:7001/db/catalog 
   -q /catalog/journal[1]/article/title

The -q switch specifies the XPath query. The output from the XPath query is:

<title src:col="/db/catalog" src:key="catalog.xml" 
xmlns:src="http://xml.apache.org/xindice/Query">
What Is a Portlet</title>

As another example, retrieve the publisher attribute of the catalog element.

>xindice xpath -c xmldb:xindice://localhost:7001/db/catalog 
    -q /catalog/@publisher

The output from the XPath query is:

<xq:result publisher="OReilly" 
xmlns:xq="http://xml.apache.org/xindice/Query" 
xq:col="/db/catalog" xq:key="catalog.xml" />

Next, query the database with the XML:DB API. Obtain the catalog collection from the database as in the "Adding an XML Document to the Database" section. Next, specify the XPath query string. The following XPath expression finds the title of the article in the first journal element.

String xpath = "/catalog/journal[1]/article/title";

The org.xmldb.api.modules.XpathQueryService service is used to query a database collection. Create an XPathQueryService object. Query the database with the XPathQueryService query() method. The query returns a org.xmldb.api.base.ResourceSet. Iterate over the resource set to obtain the XML document resource org.xmldb.api.base.Resource. Output the XML document in the resource.

XPathQueryService queryService = 
  (XPathQueryService) 
  collection.getService("XPathQueryService","1.0");
ResourceSet resourceSet = queryService.query(xpath);
ResourceIterator iterator = resourceSet.getIterator();
   while (iterator.hasMoreResources()) {
       Resource resource = iterator.nextResource();
       System.out.println(resource.getContent());
   }

The output of the XPath query is:

<title src:col="/db/catalog" 
src:key="022705cf47a9e30900000107728ed85e"
 xmlns:src="http://xml.apache.org/xindice/Query">
What Is a Portlet</title>

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow