9
How to Use XUpdate in Java with Xindice database Created by: SAIDY Fouad http://fouad-saidy.freehostia.com [email protected] Friday, March 12, 2010

How to use xupdate in java with xindice database

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: How to use xupdate in java  with xindice database

How to Use XUpdatein Java

with Xindice database

Created by:SAIDY Fouad

http://[email protected]

Friday, March 12, 2010

Page 2: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 2

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 > [email protected] < /email >< age > 22 < /age >< /person >< person id=”2″ >< fname > KEVIN < /fname >< lname > MITNICK < /lname >< email > [email protected] < /email >< age > 45 < /age >< /person >< person id=”3″ >< fname > BILL < /fname >< lname > GATES < /lname >< email > [email protected] < /email >< age > 55 < /age >< /person >< /persons >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,

Page 3: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 3

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.batTo add the Xml file to the XML database we should create a Collection in whish we will store this file:

Page 4: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 4

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

Page 5: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 5

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)

Page 6: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 6

Add Jars To Java Project

Create a simple java class called xupdate and add this code to your class: Download xupdate.javato 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 [email protected] Remove the 4th person : -R persons 4o update the second person : -E persons 2 steve Mcarty 21

[email protected] 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:

Page 7: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 7

XUpdate code sent to the server

For more information about how to use XUpdate UseCasesnow 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 TutorialCreate a simple java class called show_xml and copy this file to your class:show_xml.javaexecute this file and you will see the result in eclipse console like this :

Page 8: How to use xupdate in java  with xindice database

H o w t o U s e X U p d a t e i n J a v a w i t h X i n d i c e d a t a b a s e P a g e | 8

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.