Home
   Quick Start
   Description   NEW!
   JavaDoc
   Project at SF.net
   Downloads
   Examples
   ToDo List   NEW!
   Contact


   SourceForge.net Logo


   javaNet.net Logo

  

Quick Start Example

Simple example:  We have some XML document, let it be simple XML-file (see below) and need to change telephone to "800-333-3333" for employee with lastname= "Doe" in the file employees.xml (see below):

<employees>
<employee id="123">
<firstname>John</firstname>
<lastname>Doe</lastname>
<telephone>800-555-1212</telephone>
</employee>
<employee id="456">
<firstname>Jane</firstname>
<lastname>Smith</lastname>
<telephone>888-555-1212</telephone>
</employee>
</employees>

XTAS Solution: the PhoneModifier.java:

import xtas.*;
public class PhoneModifier
{
     public void modify(String lastName, String newPhone) throws Exception
     {
       try {
             String xPath = "/employees/employee[lastname='"+lastName+"']/telephone/text()";
             Statement qc = new SimpleStatement(QueryType.UPDATE, 
                                                                            xPath, 
                                                                            newPhone, 
                                                                            "employees.xml", "employees.xml");
              Query q = new Query( qc );
              q.execute();
              q.serialize();
        }  catch ( Exception e)  {
             throw new Exception("Error occured: "+e);
         } 
     }
      public static void main(String[] args) throws Exception {
          new PhoneModifier().modify( "Doe", "800-333-3333");
      }
}

Method PhoneModifier.modify(..) includes:

  • building of XPath expression for the querying old value of telephone element for employee with lastname "Doe"
  • creatng of XTAS UPDATE Query with this xpath expression, new value of telephone number, query data source (local file "employees.xml") and query data destination (local file "employees.xml")
  • execution of this query that will select old telephone by this XPath expression and update with new one
  • storing (serialize()) query's result in the local file "employees.xml"

You can check out the result in the employees.xml but note that for building executable you need the xtas.jar, so your building string must looks like: javac -classpath ../dist/xtas.jar PhoneModifier.java

and in run time your need (besides the xtas.jar) the xml-apis library, parser (Xerces 2) and XSLT-implementation (Xalan 2), so your run string must looks like: java -classpath .;../lib/xalan.jar;../lib/xml-apis.jar;../lib/xercesImpl.jar;../dist/xtas.jar PhoneModifier

and in the 5-th line of employees.xml you will see: <telephone>800-333-3333 </telephone>