Storing an XML Document in Apache Xindice
Pages: 1, 2, 3, 4, 5, 6
Adding an XML Document to the Database
Next, add an example XML document to the database collection, catalog. The example XML document is as follows:
<catalog title="OnJava.com" publisher="OReilly">
<journal date="Sept 2005">
<article>
<title>What Is a Portlet</title>
<author> Sunil Patil</author>
</article>
</journal>
<journal date="Sept 2005">
<article>
<title>What Is Hibernate</title>
<author>James Elliott</author>
</article>
</journal>
<journal date="Oct 2003">
<article>
<title>BCEL Maven and CSS with Swing</title>
<author>Daniel Steinberg</author>
</article>
</journal>
</catalog>
Use the command-line tool to add an XML document to the Xindice database with the command:
>xindice ad -c xmldb:xindice://localhost:7001/db/catalog
-f c:/xindice/catalog.xml -n catalog.xml
This adds the catalog.xml document to the catalog collection, printing the confirmation:
Added document
xmldb:xindice://localhost:7001/db/catalog/catalog.xml
Next, we shall add a collection with the XML:DB API. Import the XML:DB API and core server classes, which are listed in the previous section. Create and register the database driver as in the "Creating a Collection in the Database" section. Obtain the catalog collection from the database:
Collection collection = DatabaseManager.getCollection
("xmldb:xindice://localhost:7001/db/catalog");
Obtain a Document object for the XML document to be added to the database.
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
File datafile = new File("c:/Xindice/catalog.xml");
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(datafile);
Create an ID for the XML document resource to be added to the collection. The ID associates the collection with an identifier. Create a org.xmldb.api.modules.XMLResource for the XML document to add from the collection. The XMLResource object represents an XML resource, such as a Document object, in the Xindice database. Set the content of the XMLResource from the Document object. Add the XML resource to the collection.
String resourceID = collection.createId();
XMLResource resource = (XMLResource)
(collection.createResource(resourceID,
"XMLResource"));
resource.setContentAsDOM(document);
collection.storeResource(resource);
This adds the XML document to the database collection.