### Initial POUpdater Class Structure Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The initial setup for the POUpdater class, including imports and the skeleton of the addLineItem method for parsing XML. ```java import java.io.*; import java.math.*; import org.apache.xmlbeans.*; import org.openuri.easypo.*; public class POUpdater { private static String addLineItem(File purchaseOrder, String itemDescription, String perUnitOuncesString, String itemPriceString, String itemQuantityString) { // Bind the incoming XML to an XMLBeans type. PurchaseOrderDocument poDoc = null; try { poDoc = PurchaseOrderDocument.Factory.parse(purchaseOrder); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return poDoc.toString(); } } ``` -------------------------------- ### Cursor-Style Element Insertion Snippet Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html A partial example showing the basic sequence of cursor operations for inserting characters and elements. ```java cursor.insertChars(itemDescription); cursor.toNextToken(); cursor.beginElement("per-unit-ounces", namespaceUri); // ... and so on for the other children... ``` -------------------------------- ### Alphabetization Import Statements Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Required imports for the alphabetization logic used in the cursor example. ```java import java.text.*; import java.util.*; ``` -------------------------------- ### Get namespace URI Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Retrieves the URI for the default namespace using the cursor. ```java String namespaceUri = cursor.namespaceForPrefix(""); ``` -------------------------------- ### easypo.xml instance document Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Copy this XML into a file named easypo.xml and place it in the tutorials\gettingstarted\instances directory. It is an instance of the easypo schema. ```xml Gladys Kravitz
Anytown, PA
2003-01-07T14:16:00-05:00 Burnham's Celestial Handbook, Vol 1 5 21.79 2 Burnham's Celestial Handbook, Vol 2 5 19.89 2 ZipShip 0.74
``` -------------------------------- ### xpretty command to view easypo.xsd Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Run xpretty on the easypo schema to display it. On UNIX, this script is not available, so open the file in an editor instead. ```bash xpretty schemas\easypo\easypo.xsd ``` -------------------------------- ### Run POUpdater Class Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Command to execute the compiled POUpdater class with the required classpath and input arguments. ```bash java -cp tutorials\gettingstarted\classes;%XMLBEANS_HOME%\lib\xbean.jar;tutorials\gettingstarted\lib\easypo.jar POUpdater tutorials\gettingstarted\instances\easypo.xml "a new item" 5.0 20.00 6 ``` -------------------------------- ### Populate element with description Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Begins a description element within the current cursor position. ```java cursor.beginElement("description", namespaceUri); ``` -------------------------------- ### Initialize addLineItemWithCursor method Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Initializes the method to parse a purchase order document into generated types. ```java private static String addLineItemWithCursor(File purchaseOrder, String itemDescription, String perUnitOunces, String itemPrice, String itemQuantity) { PurchaseOrderDocument poDoc = null; try { poDoc = PurchaseOrderDocument.Factory.parse(purchaseOrder); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } PurchaseOrderDocument.PurchaseOrder po = poDoc.getPurchaseOrder(); } ``` -------------------------------- ### scomp command to compile easypo schema Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Compile the easypo schema into easypo.jar using scomp. The command is available on Windows and UNIX. The output JAR is placed in the tutorials\gettingstarted\lib directory. ```bash scomp -out tutorials\gettingstarted\lib\easypo.jar schemas\easypo\easypo.xsd ``` -------------------------------- ### Compile POUpdater Class Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Command to compile the POUpdater Java class using the XMLBeans library and the generated easypo.jar. ```bash javac -classpath %XMLBEANS_HOME%\lib\xbean.jar;tutorials\gettingstarted\lib\easypo.jar -d tutorials\gettingstarted\classes tutorials\gettingstarted\src\POUpdater.java ``` -------------------------------- ### Create XmlCursor Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Creates a cursor at the beginning of the XML represented by the PurchaseOrder object. ```java XmlCursor cursor = po.newCursor(); ``` -------------------------------- ### Full POUpdater Class Implementation Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The complete POUpdater class including a main method to execute the addLineItem logic. ```java import java.io.*; import java.math.*; import org.apache.xmlbeans.*; import org.apache.easypo.*; public class POUpdater { public static void main(String[] args) { File poXmlFile = new File(args[0]); String updatedPoXml = addLineItem(poXmlFile, args[1], args[2], args[3], args[4]); System.out.println(updatedPoXml); } private static String addLineItem(File purchaseOrder, String itemDescription, String perUnitOuncesString, String itemPriceString, String itemQuantityString) { ``` -------------------------------- ### Reassign cursor and begin element Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Reassigns the cursor to a specific line item and begins a new line-item element. ```java cursor = lineItem.newCursor(); cursor.beginElement("line-item", namespaceUri); ``` -------------------------------- ### Expected XML Output Snippet Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The expected XML structure added to the document after running the POUpdater class. ```xml Backyard Astronomer's Guide, The 5.0 49.95 2 ``` -------------------------------- ### Instantiate RuleBasedCollator Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Instantiates a RuleBasedCollator to compare strings for alphabetical ordering. ```java RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(new Locale("en", "US", "")); ``` -------------------------------- ### Updated Main Method Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The main method updated to invoke the addLineItemWithCursor function. ```java public static void main(String[] args) { File poXmlFile = new File(args[0]); // String updatedPoXml = addLineItem(poXmlFile, args[1], args[2], // args[3], args[4]); String updatedPoXml = addLineItemWithCursor(poXmlFile, args[1], args[2], args[3], args[4]); System.out.println(updatedPoXml); } ``` -------------------------------- ### Convenience method addNewCustomer Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Adds a new customer element to the purchase-order element and returns a Customer instance for updating its contents. ```java public abstract org.openuri.easypo.Customer addNewCustomer() ``` -------------------------------- ### Full addLineItemWithCursor Method Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The complete implementation of the addLineItemWithCursor method, which parses a purchase order, compares descriptions using a RuleBasedCollator, and inserts a new line-item element. ```java private static String addLineItemWithCursor(File purchaseOrder, String itemDescription, String perUnitOunces, String itemPrice, String itemQuantity) { PurchaseOrderDocument poDoc = null; try { poDoc = PurchaseOrderDocument.Factory.parse(purchaseOrder); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } PurchaseOrderDocument.PurchaseOrder po = poDoc.getPurchaseOrder(); // Set up the collator for alphabetizing. RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance(new Locale("en", "US", "")); XmlCursor cursor = po.newCursor(); // Get the document's URI so you can use it to insert. String namespaceUri = cursor.namespaceForPrefix(""); // Get the array of elements. LineItem[] lineItems = po.getLineItemArray(); // Loop through the element array to discover where to insert the new one. for (int i = 0; i < lineItems.length; i++) { LineItem lineItem = lineItems[i]; // Find out if the new line item's description belongs before the // current line item's. int comparison = collator.compare(itemDescription, lineItem.getDescription()); // If the comparison returns -1, then insert the new line item (and // its children) before the current one. if (comparison < 0) { cursor = lineItem.newCursor(); // Begin the new element. cursor.beginElement("line-item", namespaceUri); // Begin the new element and insert its text value. cursor.beginElement("description", namespaceUri); cursor.insertChars(itemDescription); // Move on and do the same for the other elements. cursor.toNextToken(); cursor.beginElement("per-unit-ounces", namespaceUri); cursor.insertChars(perUnitOunces); cursor.toNextToken(); cursor.beginElement("prices", namespaceUri); cursor.insertChars(itemPrice); cursor.toNextToken(); cursor.beginElement("quantity", namespaceUri); cursor.insertChars(itemQuantity); break; } } // Speed the cursor's garbage collection and return the updated XML. cursor.dispose(); return poDoc.toString(); } ``` -------------------------------- ### Parse and Update XML with PurchaseOrderDocument Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html This code demonstrates how to parse an XML document into a generated PurchaseOrderDocument type, add a new line item, and return the updated XML string. ```java PurchaseOrderDocument poDoc = null; try { // Bind the incoming XML to an XMLBeans type. poDoc = PurchaseOrderDocument.Factory.parse(purchaseOrder); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Convert incoming data to types that can be used in accessors. BigDecimal perUnitOunces = new BigDecimal(perUnitOuncesString); BigDecimal itemPrice = new BigDecimal(itemPriceString); BigInteger itemQuantity = new BigInteger(itemQuantityString); // Add the new element. LineItem newItem = poDoc.getPurchaseOrder().addNewLineItem(); newItem.setDescription(itemDescription); newItem.setPerUnitOunces(perUnitOunces); newItem.setPrice(itemPrice); newItem.setQuantity(itemQuantity); return poDoc.toString(); } } ``` -------------------------------- ### easypo.xsd schema content Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The easypo schema defines a purchase-order XML structure with customer, date, line-item, and shipper elements. It is the schema used throughout the tutorial. ```xml ``` -------------------------------- ### Java accessors for customer element Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Accessors for the customer element, which is of a user-defined schema type. The getCustomer method returns a Customer instance that can be updated to modify the element's content. ```java public abstract org.openuri.easypo.Customer getCustomer() public abstract void setCustomer( org.openuri.easypo.Customer ) ``` -------------------------------- ### Adding a New LineItem Element Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Code to convert raw input data into appropriate types and add a new line item to the purchase order document. ```java BigDecimal perUnitOunces = new BigDecimal(perUnitOuncesString); BigDecimal itemPrice = new BigDecimal(itemPriceString); BigInteger itemQuantity = new BigInteger(itemQuantityString); LineItem newItem = poDoc.getPurchaseOrder().addNewLineItem(); newItem.setDescription(itemDescription); newItem.setPerUnitOunces(perUnitOunces); newItem.setPrice(itemPrice); newItem.setQuantity(itemQuantity); ``` -------------------------------- ### Optional element check and removal methods for shipper Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Methods to check if the optional shipper element is present and to remove it. The shipper element is optional because its minOccurs is 0. ```java public boolean isSetShipper ( ) public abstract void unsetShipper ( ) ``` -------------------------------- ### Schema definition of purchase-order element Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Defines the global purchase-order element with a local complex type containing a sequence of child elements: customer, date, line-item, and shipper. The date element uses the built-in xs:dateTime type, while the others are complex types defined elsewhere in the schema. ```xml ``` -------------------------------- ### Java accessors for date element using Calendar Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Convenience accessors for the date element, returning and accepting java.util.Calendar. These are generated for built-in schema types like xs:dateTime. ```java public abstract java.util.Calendar getDate() public abstract void setDate ( java.util.Calendar ) ``` -------------------------------- ### Java accessors for date element using XmlDateTime Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html Alternative accessors for the date element using org.apache.xmlbeans.XmlDateTime, which allows conversion to Calendar, Date, and GDate. ```java public abstract org.apache.xmlbeans.XmlDateTime xgetDate() public abstract void xsetDate( org.apache.xmlbeans.XmlDateTime ) ``` -------------------------------- ### Generated Java Methods for Unbounded Elements Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html These methods are generated by the XMLBeans schema compiler to handle elements with maxOccurs="unbounded", providing array-based access and manipulation. ```java // Get or set the whole array. public abstract org.openuri.easypo.LineItem[] getLineItemArray ( ) public abstract void setLineItemArray ( org.openuri.easypo.LineItem[] ) // Get or set a single item. public abstract org.openuri.easypo.LineItem getLineItemArray ( int ) public abstract void setLineItemArray( int, org.openuri.easypo.LineItem ) // Add or remove an item. public abstract org.openuri.easypo.LineItem insertNewLineItem( int ) public abstract void removeLineItem( int ) // Get the array's size (without having to get the array, then call .length). public abstract int sizeOfLineItemArray() ``` -------------------------------- ### SchemaType Field Access Source: https://xmlbeans.apache.org/documentation/tutorial_getstarted.html The schema compiler generates a static field for accessing the underlying SchemaType instance. ```java public static final org.apache.xmlbeans.SchemaType type ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.