Folks,

*RE: *http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

The "newbie pages" could use some work. It took me ages to get the purchase-order example (with XQueries) running... I couldn't workout which JAR's I needed, so I'm posting the whole shebang for the edification of the next poor bunny.

BTW... I'm running jdk1.6.0_04 on Windows Vista... If you're running *nix you'll be used to translating everything.

*Required XmlBeans jars:
*    * xbean.jar = allways required
* jsr173_1.0_api.jar = W3C StAX API (doesn't actually seem to be required... but the doco says include it, so include it.
   * xbean_xpath.jar = XBeansXQuery and XBeansXPath delegates.

*Required Saxon jars:
*    * saxon9.jar = allways required
* saxon9-xqj.jar = javax.xml.xquery.* implementation (isn't actually required for the example)
   * saxon9-dom.jar = DOM implementation (required for XQuery)

*System (not user!) Environment Variables *- http://xmlbeans.apache.org/documentation/conInstallGuide.html

   XMLBEANS_HOME=C:\Java\lib\XmlBeans-2.4.0
   PATH=%PATH%;XMLBEANS_HOME/bin

*setClasspath.bat* - I "call" this before I compile, which effects the environment of the calling shell (ergo it's dotted).

   set CLASSES=C:\Java\home\classes

   REM SAXON does XSLT 2.0, XQuery 1.0, XPath 2.0; (W3C 23 Jan 2007).
   REM http://saxon.sourceforge.net/
   set D=C:\Java\lib\saxonb9-1-0-1j
   set SAXON=%D%\saxon9.jar;%D%\saxon9-xqj.jar;%D%\saxon9-dom.jar

   REM XMLBeans generates Java classes from an XML-Schema.
   REM http://xmlbeans.apache.org/index.html
   REM XMLBEANS 2.4.0 requires Saxon-9.0.0.4 (or later) for XQuery.
   REM XMLBEANS_HOME=C:\Java\lib\XmlBeans-2.4.0 #environment variable
   REM PATH=%PATH%;%XMLBEANS_HOME%\bin #environment variable
   set D=%XMLBEANS_HOME%\lib
   set XMLBEANS=%D%\xbean.jar;%D%\jsr173_1.0_api.jar;%D%\xbean_xpath.jar

   REM MYJARS - generated from xmlbeans for instance.
   set MYJARS=C:\Java\home\src\tutorials\xmlbeans\PurchaseOrderXmlBeans.jar

   REM set the current CLASSPATH using the above "abbreviations"
   set CLASSPATH=%CLASSES%;%MYJARS %;%XMLBEANS%;%SAXON%;

*PurchaseOrder.xml* *-* as per http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

   <po:purchase-order xmlns:po="http://openuri.org/easypo";>

       <po:customer>
           <po:name>Gladys Kravitz</po:name>
           <po:address>Anytown, PA</po:address>
       </po:customer>

       <po:date>2003-01-07T14:16:00-05:00</po:date>

       <po:line-item>
           <po:description>Burnham's Celestial Handbook, Vol
   1</po:description>
           <po:per-unit-ounces>5</po:per-unit-ounces>
           <po:price>21.79</po:price>
           <po:quantity>2</po:quantity>
       </po:line-item>
       <po:line-item>
           <po:description>Burnham's Celestial Handbook, Vol
   2</po:description>
           <po:per-unit-ounces>5</po:per-unit-ounces>
           <po:price>19.89</po:price>
           <po:quantity>2</po:quantity>
       </po:line-item>

       <po:shipper>
           <po:name>ZipShip</po:name>
           <po:per-ounce-rate>0.74</po:per-ounce-rate>
       </po:shipper>

   </po:purchase-order>

*PurchaseOrder.xsd - *as per http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

   <xs:schema
       xmlns:po="http://openuri.org/easypo";
       xmlns:xs="http://www.w3.org/2001/XMLSchema";
       targetNamespace="http://openuri.org/easypo";
       elementFormDefault="qualified">

     <xs:element name="purchase-order">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="customer" type="po:customer"/>
           <xs:element name="date" type="xs:dateTime"/>
           <xs:element name="line-item" type="po:line-item"
   minOccurs="0" maxOccurs="unbounded"/>
           <xs:element name="shipper" type="po:shipper" minOccurs="0"/>
         </xs:sequence>
       </xs:complexType>
     </xs:element>

     <xs:complexType name="customer">
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:element name="address" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>

     <xs:complexType name="line-item">
       <xs:sequence>
         <xs:element name="description" type="xs:string"/>
         <xs:element name="per-unit-ounces" type="xs:decimal"/>
         <xs:element name="price" type="xs:double"/>
         <xs:element name="quantity" type="xs:int"/>
       </xs:sequence>
     </xs:complexType>

     <xs:complexType name="shipper">
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:element name="per-ounce-rate" type="xs:decimal"/>
       </xs:sequence>
     </xs:complexType>

   </xs:schema>

*Build PurchaseOrderXmlBeans.jar *- http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html#scomp

   C:\>cd /d C:\Java\home\src\tutorials\xmlbeans

   C:\Java\home\src\tutorials\xmlbeans>scomp -out
   PurchaseOrderXmlBeansTest.jar PurchaseOrder.xsd
   Time to build schema type system: 0.75 seconds
   Time to generate code: 0.125 seconds
   Time to compile code: 1.266 seconds
   Compiled types to: PurchaseOrderXmlBeansTest.jar

   C:\Java\home\src\tutorials\xmlbeans>

*PurchaseOrderXmlBeansTest.java* - The "compiled" code samples from: http://xmlbeans.apache.org/documentation/tutorial_getstarted.html ... Tested on 1.6 but should be OK on 1.4

   package tutorials.xmlbeans;

   import java.io.File;
   import org.apache.xmlbeans.*;
   import org.openuri.easypo.PurchaseOrderDocument;
   import org.openuri.easypo.PurchaseOrderDocument.PurchaseOrder;
   import org.openuri.easypo.LineItem;
   import org.openuri.easypo.Customer;

   import org.apache.xmlbeans.XmlException;
   import java.io.IOException;

   public class PurchaseOrderXmlBeansTest
   {
     private PurchaseOrderDocument doc;

     PurchaseOrderXmlBeansTest(File purchaseOrdersXml) throws
   org.apache.xmlbeans.XmlException, java.io.IOException {
       doc = PurchaseOrderDocument.Factory.parse(purchaseOrdersXml);
     }

     public void printLineItems() {

       PurchaseOrder purchaseOrder = doc.getPurchaseOrder();

       /*
        * When an element may occur more than once as a child element,
        * the schema compiler will generate methods that refer to an
        * array of that element. The line-item element is defined with
        * a maxOccurs attribute value of "unbounded", meaning that
        * it may occur as many times in an instance document as needed.
        * So there are methods such as getLineItemArray and
   setLineItemArray.
        */
       LineItem[] lineItems = purchaseOrder.getLineItemArray();
       System.out.println("Purchase order has " + lineItems.length + "
   line items.");

       /*
        * Loop through the line-item elements, using generated accessors to
        * get values for child elements such a description, quantity, and
        * price.
        */
       double totalPrice = 0.0;
       int totalQty = 0;
       for (int i=0; i<lineItems.length; i++) {
         LineItem item = lineItems[i];
         double itemQty   = item.getQuantity();
         double itemPrice = item.getPrice();
         double itemTotal = itemQty * itemPrice;
         totalQty += itemQty;
         totalPrice += itemTotal;
         System.out.println("Line item      : "+i);
         System.out.println("   Description : "+item.getDescription());
         System.out.println("   Quantity    : "+itemQty);
         System.out.println("   Price       : "+itemPrice);
         System.out.println("   Total       : "+itemTotal);
       }
       System.out.println("Total items  : " + totalQty);
       System.out.println("Total amount : " + totalPrice);
       System.out.println();
     }

     /**
      * Creates a new purchase-order element and adds a customer child
   element.
      * It then inserts name and address child elements, creating the
   elements
      * and sets their values with a single call to their setter methods.
      *  <ns1:purchase-order xmlns:ns1="http://openuri.org/easypo";>
      *      <ns1:customer>
      *          <ns1:name>Doris Kravitz</ns1:name>
      *          <ns1:address>Bellflower, CA</ns1:address>
      *      </ns1:customer>
      *  </ns1:purchase-order>
      */
     public static PurchaseOrderDocument
   createPurchaseOrderDocument(String customerName, String
   customerAddress) {
       PurchaseOrderDocument newDoc =
   PurchaseOrderDocument.Factory.newInstance();
       PurchaseOrder newPo = newDoc.addNewPurchaseOrder();
       Customer newCustomer = newPo.addNewCustomer();
       newCustomer.setName(customerName);
       newCustomer.setAddress(customerAddress);
       return newDoc;
     }

     /**
      * Prints all of the line-item elements whose price child elements
   have
      * values less than or equal to the given maxPrice.
      */
     public void printCheapItems(double maxPrice) {
       // The XQuery expression combines a namespace and a path. The
   namespace
       // defines the XML-namespace of the following query expression.
       String namespace = "declare namespace po =
   'http://openuri.org/easypo'; ";
       String path = "$this/po:purchase-order/po:line-item[po:price <=
   "+maxPrice+"]";
       String query = namespace + path;
       XmlCursor itemCursor = doc.newCursor().execQuery(query);
       System.out.println("Cheap items: "+itemCursor.xmlText());
     }

     /**
      * Uses an XML cursor to print the customer's name.
      * po:purchase-order/po:customer/po:name/TextNode
      */
     public void printCustomersName() {
       XmlCursor cursor = doc.newCursor();
       cursor.toChild(0);
       cursor.toChild(0);
       cursor.toChild(0);
       System.out.println("Customers name: "+cursor.getTextValue());
       cursor.dispose();
     }

     public static void main(String[] args) {
       try {
         PurchaseOrderXmlBeansTest test = new
   PurchaseOrderXmlBeansTest(new File("PurchaseOrder.xml"));
         test.printLineItems();
         test.printCheapItems(10.00);
         test.printCustomersName();

         PurchaseOrderDocument newDoc =
   test.createPurchaseOrderDocument("Bob The Builder", "123 Bulldozer St");
         System.out.println("New document: "+newDoc.xmlText());
       } catch (Exception e) {
         e.printStackTrace();
       }
     }

   }

*Compile:*

   C:\Java\home\src\tutorials\xmlbeans>C:\Java\jdk\jdk1.6.0_04\bin\javac.exe
   -Xlint -d C:\Java\home\classes -cp
   
c:\java\home\src;.;C:\Java\home\classes;C:\Java\home\src\tutorials\xmlbeans\PurchaseOrderXmlBeans.jar;C:\Java\lib\xmlbeans-2.4.0\lib\xbean.jar;C:\Java\lib\xmlbeans-2.4.0\lib\xbean_xpath.jar;C:\Java\lib\saxonb9-1-0-1j\saxon9.jar;C:\Java\lib\saxonb9-1-0-1j\saxon9-dom.jar;
   PurchaseOrderXmlBeansTest.java

*Run:*

   C:\Java\home\src\tutorials\xmlbeans>C:\Java\jdk\jdk1.6.0_04\bin\java.exe
   -enableassertions -cp
   
C:\Java\home\classes;C:\Java\home\src\tutorials\xmlbeans\PurchaseOrderXmlBeans.jar;C:\Java\lib\xmlbeans-2.4.0\lib\xbean.jar;C:\Java\lib\xmlbeans-2.4.0\lib\xbean_xpath.jar;C:\Java\lib\saxonb9-1-0-1j\saxon9.jar;C:\Java\lib\saxonb9-1-0-1j\saxon9-dom.jar;
   tutorials.xmlbeans.PurchaseOrderXmlBeansTest

   Purchase order has 2 line items.
   Line item      : 0
      Description : Burnham's Celestial Handbook, Vol 1
      Quantity    : 2.0
      Price       : 21.79
      Total       : 43.58
   Line item      : 1
      Description : Burnham's Celestial Handbook, Vol 2
      Quantity    : 2.0
      Price       : 19.89
      Total       : 39.78
   Total items  : 4
   Total amount : 83.36

   Cheap items: <xml-fragment/>
   Customers name: Gladys Kravitz
   New document: <eas:purchase-order
   xmlns:eas="http://openuri.org/easypo";><eas:customer><eas:name>Bob
   The Builda</eas:name><eas:address>123 Bulldozer
   St</eas:address></eas:customer></eas:purchase-order>


Hope this helps someone out...

Cheers. Keith.

Reply via email to