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

Deleting an Element with XML:DB API

Similarly, you remove a journal element from the XML document in the database with the xupdate:remove command. To remove the first journal element, you create a XUpdate command, String. Update the XML document with the XUpdate query service:

xupdate = "<xupdate:modifications version=\"1.0\"" +
    "    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
    "    <xupdate:remove select=\"/catalog/journal[1]\"/>" +
    "</xupdate:modifications>"; queryService.update(xupdate);

Modifying an Element with XML:DB API

Next, modify an element with the xupdate:update command. Modify the title of the article in the second journal element. Again, we'll create a XUpdate command String to update the XML document with the XUpdate query service.

xupdate = "<xupdate:modifications version=\"1.0\"" +
"    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
"    <xupdate:update select=\"/catalog/journal[2]/article/title\"> +
"    Maven with Swing</xupdate:update>" +
"</xupdate:modifications>";

queryService.update(xupdate);

Retrieve the modified XML document in the database collection catalog. The modified XML document is listed:

<?xml version="1.0"?>
<catalog publisher="OReilly" title="OnJava.com"> 
 <journal date="Sept 2005">
  <article> 
   <title>What Is Hibernate</title>
   <author>James Elliott</author>
  </article>
 </journal>
 <journal date="Oct 2003">
  <article> 
   <title>Maven with Swing</title>
   <author>Daniel Steinberg</author>
  </article>
 </journal><journal date="Aug 2005">    
  <article>    
   <title>iBatis DAO</title> 
   <author>Sunil Patil</author>   
  </article>    
 </journal>    
</catalog>

Deleting an XML Document

Next, delete an XML document from the database collection. With the command-line tool, delete the XML document with the command:

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

This deletes the XML document catalog.xml in the catalog collection with the message:

DELETED: xmldb:xindice://localhost:7001/db/catalog/catalog.xml

Next, delete the XML document with the XML:DB API. You need to obtain the catalog collection from which an XML document is to be deleted, and obtain the XML resource to delete. Delete the XML resource as follows:

XMLResource resource = (XMLResource) 
  (collection.getResource(resourceID));
collection.removeResource(resource);

This deletes the selected XML document from the database.

The Xindice Java application XIndiceDB.java, used to create a collection, add an XML document, retrieve an XML document, query the database, update the database, and delete an XML document, is available in the xindice-resources.zip file.

Conclusion

In this tutorial, the Xindice database server is configured with the WebLogic server 9.0. Another application server, such as JBoss, may also be used to configure Xindice. JDK 5.0 was used, because JDK 1.4.2 generates an exception with Xindice 1.1b4. WebLogic version 9.0 is used because version 9.0 supports JDK 5.0.

Resources:

Deepak Vohra is a NuBean consultant and a web developer.


Return to ONJava.com.