How to Use XUpdate in Java with Xindice database

Created by: SAIDY Fouad http://fouad-saidy.freehostia.com fouadsaidy@gmail.com

Friday, March 12, 2010

How to Use XUpdate in Java with Xindice database |2

Page

The aim of this tutorial is to show you how to develop a simple java class that use CRUD (Create Read Update Delete) operations with an xml file stored in a native XML Database. Firstly to begin you should download those tools: • Eclipse Galileo • Xindice 1.0 DataBase • Xindice Browser 1. Launching the server and adding the XML file to the native database : let’s begin our project ,we should create a xml file for example called persons.xml in whish we add some person elements ,each person has different nodes(first name, last name, email ,age) this is an example of persons.xml : < persons > < person id=”1″ > < fname > FOUAD < /fname > < lname > SAIDY < /lname > < email > fouadsaidy@gmail.com < /email > < age > 22 < /age > < /person > < person id=”2″ > < fname > KEVIN < /fname > < lname > MITNICK < /lname > < email > kevin.mitnick@gmail.com < /email > < age > 45 < /age > < /person > < person id=”3″ > < fname > BILL < /fname > < lname > GATES < /lname > < email > bill.gates@gmail.com < /email > < age > 55 < /age > < /person > < /persons >

How to Use XUpdate in Java with Xindice database |3

Page

We created the xml file let’s now store this file in Xindice database ,to do that we should start the server, open the Xindice Folder called xindice-1.0 –>launch startup.bat, to verify if the server is executed correctly open your navigator and show http://localhost:4080/ page if you saw an xindice page that means the server is launched without errors.

The Server Is Running

Apache XIndice Start Page Next step is to launch Xindice Browser which give us many options to look to the database (open XindiceBrowser and execute XIndiceBrowser.bat To add the Xml file to the XML database we should create a Collection in whish we will store this file:

How to Use XUpdate in Java with Xindice database |4

Page

Create a Test Collection Now let’s add persons.xml file to the new collection:

Add XML File To Test Collection

Add persons.xml to Test collection

How to Use XUpdate in Java with Xindice database |5

Page

show persons.xml in XIndice Browser We finished the first step of configuration and insertion of XML file in native database, next step is to create a java class wishes manipulate the XML file stored in the database. 2. Update XML data base in java open Eclipse Galileo IDE and create a new Java Project called test_xindice ,click the right mouse into the project and select Properties , in the properties window select Java Build Path –> Libraries , then click the Add External JARs… Button, select the 5 jar’s in the java–> lib folder (see the next image)

How to Use XUpdate in Java with Xindice database |6
Add Jars To Java Project

Page

Create a simple java class called xupdate and add this code to your class: Download xupdate.java to execute this class we should modify the run configuration of the eclipse IDE to do that select in principal menu run–>run configuration–>Arguments, enter one of this arguments: o add a new person : -A persons farid safiri 25 farid@gmail.com o Remove the 4th person : -R persons 4 o update the second person : -E persons 2 steve Mcarty 21 sMcarty@gmail.com after writhing one of those case in the argument section click run, you will see the xupdate code in the eclipse console, for example if we write the first option adding a new person to the persons.xml file stored in the database we will see the request like this :

Add Arguments to Eclipse run configuration The code produced by the program and executed in the server:

How to Use XUpdate in Java with Xindice database |7

Page

XUpdate code sent to the server For more information about how to use XUpdate UseCases now let’s see the modification in Xml database, move to XIndice Database Browser click the refresh button and select persons.xml file you will see that the forth person is added to the database :

XML Database updated In the same way you can execute author options of removing or updating an element in the XML database.

3.

Execute XPath expression in java

To learn XPath : XPath Tutorial Create a simple java class called show_xml and copy this file to your class: show_xml.java execute this file and you will see the result in eclipse console like this :

How to Use XUpdate in Java with Xindice database |8

Page

XPath Result Finally you achieved the mission of updating a XML Database and showing the XPath Result using java and Xindice Database, you can now create more complex application based on XML database. If you have any comments or remarks don’t hesitate to post it in my blog, or alert me by email.

Sign up to vote on this title
UsefulNot useful