| Sign In/My Account | View Cart |
Storing an XML Document in Apache Xindice
Pages: 1, 2, 3, 4, 5, 6
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);
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>
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.
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.
Deepak Vohra is a NuBean consultant and a web developer.
Return to ONJava.com.
Showing messages 1 through 9 of 9.
import org.apache.xindice.client.xmldb.services.*;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.xml.dom.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class XIndiceDB {
private Collection collection;
private Collection catalogCollection;
String resourceID;
public void createCollection() {
try {
String xindiceDriver = "org.apache.xindice.client.xmldb.DatabaseImpl";
Database xindiceDatabase = (Database) ((Class.forName(xindiceDriver)).newInstance());
DatabaseManager.registerDatabase(xindiceDatabase);
String url = "xmldb:xindice://localhost:7001/db";
collection = DatabaseManager.getCollection(url);
String collectionName = "catalog";
CollectionManager collectionManagerService = (CollectionManager) collection.getService("CollectionManager",
"1.0");
String collectionConfig = "<collection compressed=\"true\" " +
" name=\"" + collectionName + "\">" +
" <filer class=\"org.apache.xindice.core.filer.BTreeFiler\"/>" +
"</collection>";
catalogCollection = collectionManagerService.createCollection(collectionName,
DOMParser.toDocument(collectionConfig));
} catch (XindiceException e) {
} catch (XMLDBException e) {
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
}
public void addDocument() {
try {
String xindiceDriver = "org.apache.xindice.client.xmldb.DatabaseImpl";
Database xindiceDatabase = (Database) ((Class.forName(xindiceDriver)).newInstance());
DatabaseManager.registerDatabase(xindiceDatabase);
collection = DatabaseManager.getCollection(
"xmldb:xindice://localhost:7001/db/catalog");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
File datafile = new File("c:/XIndice/catalog.xml");
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(datafile);
resourceID = collection.createId();
XMLResource resource = (XMLResource) (collection.createResource(resourceID,
"XMLResource"));
resource.setContentAsDOM(document);
collection.storeResource(resource);
}
catch (SAXException e) {
} catch (ParserConfigurationException e) {
} catch (XMLDBException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (InstantiationException e) {
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
}
}
public void retrieveDocument() {
try {
XMLResource resource = (XMLResource) (collection.getResource(resourceID));
System.out.println(resource.getContent());
} catch (XMLDBException e) {
}
}
public void queryDocument() {
try {
String xpath = "/catalog/journal[1]/article/title";
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());
}
} catch (XMLDBException e) {
}
}
public void updateDocument() {
try {
String xindiceDriver = "org.apache.xindice.client.xmldb.DatabaseImpl";
Database xindiceDatabase = (Database) ((Class.forName(xindiceDriver)).newInstance());
DatabaseManager.registerDatabase(xindiceDatabase);
collection = DatabaseManager.getCollection(
"xmldb:xindice://localhost:7001/db/catalog");
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>";
XUpdateQueryService queryService = (XUpdateQueryService) collection.getService("XUpdateQueryService",
"1.0");
queryService.update(xupdate);
xupdate = "<xupdate:modifications version=\"1.0\"" +
" xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" +
" <xupdate:remove select=\"/catalog/journal[1]\"/>" +
"</xupdate:modifications>";
queryService.update(xupdate);
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);
XMLResource resource = (XMLResource) (collection.getResource(resourceID));
System.out.println(resource.getContent());
} catch (XMLDBException e) {
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
} catch (InstantiationException e) {
System.out.println(e.getMessage());
} catch (IllegalAccessException e) {
System.out.println(e.getMessage());
}
}
public void deleteDocument() {
try {
XMLResource resource = (XMLResource) (collection.getResource(resourceID));
collection.removeResource(resource);
} catch (XMLDBException e) {
}
}
public static void main(String[] argv) {
XIndiceDB xindicedb = new XIndiceDB();
xindicedb.createCollection();
xindicedb.addDocument();
xindicedb.retrieveDocument();
xindicedb.queryDocument();
xindicedb.updateDocument();
xindicedb.deleteDocument();
}
}
xindice-1.1b4.war with xindice.war in 1.server.xml with system.xml and xindice.jar with xindice.war in 2.server.xml with system.xml in 3.xindice.jar with xindice.war and server.xml with system.xml in 4.xindice.jar with xindice.war in 5.xindice-1.1b4.jar with xindice-1.1b4.war in 1.>xindice ac -c xmldb:xindice://localhost:7001/db -n adenine>xindice ld -c xmldb:xindice://localhost:7001/db/adenine>xindice rd -c xmldb:xindice://localhost:7001/db/catalog
-n adenine.xml rd action.
1.Install JDK 5.0
2.In
jboss-4.0.2\bin\runbatch file setJAVA_HOMEto J2SE 5.0 JDK.3. Modify
<Xindice>/Xindice-webapp/xindice-1.1b4/xindice-1.1b4.warto xindice.war.4. Copy the
xindice.warfile to the\jboss-4.0.2\server\default\deploydirectory.5. Start the JBoss server. This starts the Xindice database server in the JBoss server and opens the default Xindice database, db. The Xindice server web application gets deployed in the JBoss server.
6. The Xindice server URL is shown as
http://localhost:/8080/xindice.7. Collection context for xindice server is
-c xmldb:xindice://localhost:8080/db