HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"XML & Java: The Why and the How"
Volume: 4 Issue: 9, p. 8

	

Listing 1: 

<?xml version="1.0"?> 
<PurchaseRequest> 
  <BuyerName>TonyElectronics</BuyerName> 
  <BuyerAddress> 
    <StreetName>909 E. Las Marias</StreetName> 
    <City>Dallas</City> 
    <State>Texas</State> 
    <ZipCode>75090</ZipCode> 
  </BuyerAddress> 
  <OrderNumber Quantity="500"> 
    <ProductID>PC102</ProductID> 
    <ProductName>Siega 400MHz Pentium PC</ProductName> 
    <DeliveryDate>November 19, 1999</DeliveryDate> 
    <RequestedPrice>$1200</RequestedPrice> 
  </OrderNumber> 
</PurchaseRequest> 

Listing 2: 

Document doc; 
TXElement root; 
Buyer B; 
Parser p = new Parser("XMLParser"); 
try { 
    FileInputStream file = new FileInputStream(documentName); 
    doc = p.readStream(file); 
    root = (TXElement) doc.getDocumentElement(); 
    TXElement name = (TXElement) root.getElementNamed("Buyer 
    Name"); 
    TXElement address = (TXElement)root.getElementNamed("Buy 
    erAddress"); 
    TXElement street =   (TXElement)address.getElement 
    Named("StreetName"); 
    TXElement city = (TXElement)address.getElement- 
    Named("City"); 
    TXElement state = (TXElement)address.getElement- 
    Named("State"); 
    TXElement zip = (TXElement) address.getElementNamed("Zip 
    Code"); 
    B = new Buyer(name, street, city, state, zip); 
} 
catch (java.io.IOException e) { 
       e.printStackTrace(); 
} 

Listing 3: 

try{ 
     Parser parser = ParserFactory.makeParser(parserClass); 
     saxTest handler = new saxTest(); 
     parser.setDocumentHandler(handler); 
     parser.setErrorHandler(handler); 
     parser.parse(args[0]); 
     System.exit(0); 
} 
catch (Exception e) { 
     e.printStackTrace(); 
} 

public void startElement(String name, AttributeList atts) { 
     if (name.equals("OrderNumber")) 
     { 
       int qty = Integer.parseInt(atts.getValue("Quantity")); 
       if (qty >= 500) 
       { 
            processOrder(filename); 
       } 
   } 
} 



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.