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

Modifying the Database with XUpdate

Xindice implements the XML:DB XUPdate mechanism to update an XML document. Let's update the XML document in the database with XUpdate. First, we'll use the command-line tool. The xindice command xupdate action is used to update an XML document. Some of the XUpdate commands to update an XML document are listed in Table 4.

XUpdate Command Description
xupdate:insert-after Adds a node after the selected node.
xupdate:update Updates the selected node.
xupdate:remove Removes the selected node.

Table 4. XUpdate commands

Adding an Element with the xindice Command

For example, we'll update the catalog.xml file to add a journal element to the XML document. The elements/attributes to be updated/added are specified in the xupdate configuration file, xupdate.xml. The xupdate.xml configuration file to add a journal element is as follows:

<xupdate:modifications version="1.0"
  xmlns:xupdate="http://www.xmldb.org/xupdate">
 <xupdate:insert-after select="/catalog/journal[3]">
  <xupdate:element name="journal">
  <xupdate:attribute name="date">Aug 2005</xupdate:attribute>
   <article> 
    <title>iBatis DAO</title>
    <author>Sunil Patil</author>
   </article>
  </xupdate:element>
 </xupdate:insert-after>
</xupdate:modifications>

Update the XML document with the command:

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

The xupdate action specifies an XML document is to be updated. The -f switch specifies the xupdate.xml configuration file. This updates XML document with the message:

1 documents updated

Deleting and Modifying an Element with the xindice Command

As another example, remove a journal element and modify the title in another journal element. Let's remove the first journal element and modify the title in the third journal element. Since the first journal element is removed before the third journal element is updated, the journal element to be updated becomes the second journal element. We use xupdate:remove to remove an element and xupdate:update to update an element. Here's the xupdate.xml configuration file for removing and modifying elements:

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

Update the XML document with the command:

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

This updates the XML document in the Xindice database.

Adding an Element with XML:DB API

Next, update the example XML document with the XML:DB API. As an example, add a journal element after the third journal element. Obtain the catalog collection from the database as in the "Adding an XML Document to the Database" section. Then specify the XUpdate commands in an XUpdate string.

String xupdate = 
"<xupdate:modifications version=\"1.0\"" +
"    xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
"    <xupdate:insert-after select=\"/catalog/journal[3]\">" +
"    <journal date=\"Aug 2005\">" + " <article>" +
"    <title>iBatis DAO</title>" +
"    <author>Sunil Patil</author>" + " </article>" +
"    </journal>" + "    </xupdate:insert-after>" +
"</xupdate:modifications>";

The org.xmldb.api.modules.XUpdateQueryService is used to update the database with XUpdate. Create a XUpdateQueryService object from the collection to update. Update the database with the update() method of the XUpdateQueryService object, as follows:

XUpdateQueryService queryService = 
(XUpdateQueryService) collection.getService("XUpdateQueryService",
                   "1.0");
queryService.update(xupdate);

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

Next Pagearrow