The example in this section uses the OrderXml class and its methods for basic operations on XML Order documents. The source code and Javadoc specifications for OrderXml are in $ASDIR/sample/JavaSql.
OrderXml is a subclass of the JXml class, which is specialized for XML Order documents. The OrderXml constructor validates the document for the Order DTD. Methods of the OrderXml class support referencing and updating the elements of the Order document.
Constructor: OrderXml(String)
Validates that the String argument contains a valid XML Order document, then constructs an OrderXml object containing that document. For example, “doc” is a Java string variable containing an XML Order document, perhaps one read from a file:
jcs.xml.orders.OrderXml ox = new jcs.xml.orders.OrderXml(doc);
Constructor: OrderXml(date, customerId, dtdOption, server)
The parameters are all String.
This method assumes a set of SQL tables containing Order data. The method uses JDBC to execute a SQL query that retrieves Order data for the given date and customerId. The method then assembles an XML Order document with the data.
The server parameter identifies the IQ server on which to execute the query.
If you invoke the method in a client environment, specify the server name.
If you invoke the method in Sybase IQ (in a SQL statement or in dbisql), specify either an empty string or the string “jdbc:default:connection,” which indicates that the query should be executed on the current IQ server.
The dtdOption parameter indicates whether the generated Order contains the DTD or the DTD is referenced externally.
For example:
jcs.xml.orders.OrderXml ox = new OrderXml(“990704”, “123”, “external”, “antibes:4000?user=DBA&password=SQL”);
void order2Sql(String ordersTableName, String server)
Extracts the elements of the Order document and stores them in a SQL table created by the createOrdertable( ) method. ordersTableName is the name of the target table. The server parameter is as described for the OrderXml constructor. For example, if ox is a Java variable of type OrderXml:
ox.order2Sql(“current_orders”, “antibes:4000?user=DBA&password=SQL”);
This call extracts the elements of the Order document contained in ox, and uses JDBC to insert the extracted elements into rows and columns of the table named current_orders.
static void createOrderTable(String ordersTableName, String server)
Creates a SQL table with columns suitable for storing Order data: customer_id, order_date, item_id, quantity, and unit. ordersTableName is the name of the new table. The server parameter is as described for the OrderXml constructor. For example:
jcs.xml.orders.OrderXml.createOrderTable (“current_orders”, “antibes:4000?user=DBA&password=SQL”);
String getOrderElement(String elementName)
elementName is “Date,” “CustomerId,” or “CustomerName.” The method returns the text of the element. For example, if ox is a Java variable of type OrderXml:
String customerId = ox.getOrderElement(“CustomerId”); String customerName = ox.getOrderElement(“CustomerName”); String date = ox.getOrderElement(“Date”);
void setOrderElement(String elementName, String newValue)
elementName is as described for getOrderElement( ).The method sets that element to newValue. For example, if ox is a Java variable of type OrderXml:
ox.setOrderElement(“CustomerName”, “Acme Alpha Consolidated”); ox.setOrderElement(“CustomerId”, “987a”); ox.setOrderElement(“Date”, “1999/07/05”);
String getItemElement(int itemNumber, String elementName)
itemNumber is the index of an item in the order. elementName is “ItemId,” “ItemName,” or “Quantity.” The method returns the text of the item. For example, if ox is a Java variable of type OrderXml:
String itemId = ox.getItemElement(2, “ItemId”); String itemName = ox.getItemElement(2, “ItemName”); String quantity = ox.getItemElement(2, “Quantity”);
void setItemElement(int itemNumber, String elementName, String newValue)
itemNumber and elementName are as described for the getItemElement method. setItemElement sets the element to newValue. For example, if ox is a Java variable of type OrderXml:
ox.setItemElement(2, “ItemId”, “44”); ox.setItemElement(2, “ItemName”, “cord”); ox.setItemElement(2, “Quantity”, “3”);
String getItemAttribute(int itemNumber, elementName, attributeName)
itemNumber and elementName are described as for getItemElement( ). elementName and attributeName are both String. attributeName must be “unit.” The method returns the text of the unit attribute of the item.
Since the Order documents currently have only one attribute, the attributeName parameter is unnecessary. It is included to illustrate the general case, for example, if ox is a Java variable of type OrderXml:
String itemId = ox.getItemAttribute(2, “unit”);
void setItemAttribute (int itemNumber, elementName, attributeName, newValue)
itemNumber, elementName, and attributeName are as described for getItemAttribute( ). elementName, attributeName, and newValue are String. The method sets the text of the unit attribute of the item to newValue. For example, if ox is a Java variable of type OrderXml:
ox.setItemAttribute(2, “unit”, “13”);
void appendItem(newItemId, newItemName, newQuantity, newUnit)
The parameters are all String. The method appends a new item to the document, with the given element values. For example, if ox is a Java variable of type OrderXml:
ox.appendItem(“77”, “spacer”, “5”, “12”);
void deleteItem(int itemNumber)
itemNumber is the index of an item in the order. The method deletes that item. For example, if ox is a Java variable of type OrderXml:
ox.deleteItem(2);