XML Java Analysis (1) (3)

zhaozj2021-02-17  47

A simple Java object For more complex XML documents, we need to map a series of objects to Java. Mapping a series of objects is just like a bar service student: When a waiter has to fill a row of beer, he usually allows the barrel's faucet to turn, he just moves the cup in turn next to the faucet. This is exactly what we have to capture a series of objects. We can't control the SAX events; they are like a beer that cannot be turned off. In order to solve the problem, we need to provide empty containers to make them full and keep them.

Let's describe this technology next example. With an XML document, a virtual order customer will map the XML representing a series of order items to a Java's order item vector table. The key to achieving this idea is "current goods." Every time we get an event indicating a new order item (StartElement of the OrderItem tag), we create an empty ORDER-ITEM object, join it in the list of order items, and to order goods for the current. The rest of the work is done by the XML parser.

First of all, here there is an XML document representing our virtual customer:

Bob hustead abc.123 < OrderItem> 1 48.GH605A PET Rock 19.99 12 47.9906Z Bazooka Bubble Gum 0.33 2 47.7879H < / ProductCode> Flourescent Orange Squirt Gun 2.50

It is our simple customer:

Package Common; Import Java.io. *; // Customer is a simple class that contains properties of a virtual customer. // It has a simple way to print from a print flow. public class Customer {// Customer member variables public String firstName = ""; public String lastName = ""; public String custId = ""; public void print (PrintStream out) {out.println ( "Customer:"); out. Println ("First Name -> FirstName); Out.println (" Last Name -> " LastName); Out.println (" Customer ID -> " CustId;}} next, a Simple Class To Repesent AN ORDER ITEM:

Package Common; Import Java.io. *; // Customer is a simple class that contains properties of a virtual customer. // It has a simple way to print from a print flow. public class OrderItem {// OrderItem member variables public int quantity = 0;. public String productCode = ""; public String description = ""; public double price = 0.0; public void print (PrintStream out) {out.println ( "OrderItem : "); OUT.PRINTLN (" Quantity -> "); Out.println (" Product Code -> " ProductCode); Out.println (" Description -> " Description; OUT; OUT .println ("Price ->" double.tostring (price));}}

Now, we transfer your attention to the SAX parser four, which will map customers and goods:

Import org.xml.sax. *; import org.xml.sax.helpers. *; import java.io. *; import java.util. *; import common. *; public class example4 extends defaulthandler {// for collecting Customer's XML Data Local Customer Variable Private Customer Cust = New Customer (); // Ordering the local vector table of the object. . . Private Vector ORDERITEMS = New Vector (); // Local reference for the current order object. . . Private OrderItem CurrentOrderItem; // is used to collect data from the "Characters" SAX event. Private CharRaywriter Contents = New CharaRrayWriter (); // Reserved the method of the DefaultHandler class to intercept the SAX event. // / / Refer to Org.xml.sax.contenthandler.

// Public void StartElement (String Namespaceuri, String LocalName, String Qname, Attributes Attr) throws saxexception {contents.reset (); // Newly added code. . . if (localName.equals ( "OrderItem")) {currentOrderItem = new OrderItem (); orderItems.addElement (currentOrderItem);}} public void endElement (String namespaceURI, String localName, String qName) throws SAXException {if (localName.equals ( "Firstname")) {cust.firstname = contents.tostring ();} if (localname.equals ("lastname")) {cust.lastname = contents.tostring ();} if (localname.equals ("custid") ) {Cust.custid = contents.toString ();} if ("Quantity)) {currentorderItem.quantity = integer.Valueof ().Tostring (). Trim ()). IntValue ();} (localName.equals ( "ProductCode")) {currentOrderItem.productCode = contents.toString ();} if (localName.equals ( "Description")) {currentOrderItem.description = contents.toString ();} if (localName.equals ("Price")) { CurrentOrderItem.Price = double.Valueof (). Trim ()). DoubleValue ();}} public vid characters (char [] ch, int start, int length) throws saxexception {Contents.write (ch, start , Length);} public customer getcustom () {returnit;} public vector getorderItems () {return OrderItems;

} Public static void main (string [] argv) {system.out.println ("eXample4:"); try {// Create a SAX 2 parser. . . XmlReader XR = XmlReaderFactory.createxmlReader (); // Install ContentHandler. . . Example4 ex4 = new example4 (); xr.setContentHandler (ex4); // parsing files. . . Xr.Parse (New FileRead (New FileReader)); // Displays Customer to the standard output. . . Customer Cust = EX4.GETCUSTOMER (); Cust.Print (System.out); // Display all of the order items to the standard output. . . ORDERITEM I; Vector ITEMS = EX4.GETORDERITEMS (); Enumeration E = items.Elements (); while (E.hasMoreElements ()) {i = (OrderItem) E.NEXTELEMENT (); I.Print (System.out); }}}} Catch (eXception e) {E.PrintStackTrace ();}}} This is the output generated by our Customer and ORDERITEMS object:

Example4: Customer: First Name -> Bob Last Name -> Hustead Customer Id -> abc.123OrderItem: Quantity -> 1 Product Code -> 48.GH605A Description -> Pet Rock price -> 19.99OrderItem: Quantity -> 12 Product Code -> 47.9906z Description -> Bazooka Bubble Gum Price -> 0.33ORDERITEM: Quantity -> 2 Product Code -> 47.7879h Description -> Fluorescent Orange Squirt Gun Price -> 2.5

When the structure of the XML document becomes more complicated, the real factor is difficult to manage the empty object that creates the data stream generated by the SAX event. This management is not complicated for simple object containers. However, we are developing a complex nesting container, container for a container and container containing a container member object.

(Endlessly)

转载请注明原文地址:https://www.9cbs.com/read-30176.html

New Post(0)