Client access to order elements

The main( ) method of the OrderElements class is executed on the client. It reads the Order.xml file into a local variable, and constructs an OrderXml document from it. The method then extracts the “header” elements (Date, CustomerId, and CustomerName) and the elements of the first item of the order, prints those elements, and finally updates those elements of the order with new values.

import java.io.*;
import util.*;
public class OrderElements {
 	public static void main ( String[] args) {
 		try{
		String xml = FileUtil.file2String("Order.xml");
 		xml.order.OrderXml ox = 
 			new xml.order.OrderXml(xml);
		// Get the header elements
 		String cname = ox.getOrderElement("CustomerName");
 		String cid   = ox.getOrderElement("CustomerId");
 		String date = ox.getOrderElement("Date");
		// Get the elements for item 1 (numbering from 0)
 		String iName1 = ox.getItemElement(1, "ItemName");
 		String iId1 = ox.getItemElement(1, "ItemId");
 		String iQ1 = ox.getItemElement(1, "Quantity");
 		String iU = ox.getItemAttribute(1,  "Quantity", "unit");     
 		System.out.println("\nBEFORE UPDATE: ")
 		System.out.println("\n   "+date+ "  "+ cname + " " +cid);
 		System.out.println("\n   "+ iName1+" "+iId1+" "
 			+ iQ1 + " " + iU + "\n"); 
		//  Set the header elements 
 		ox.setOrderElement("CustomerName", "Best Bakery"
 		ox.setOrderElement("CustomerId", "531");
 		ox.setOrderElement("Date", "1999/07/31");
		//  Set the elements for item 1 (numbering from 0)    
 		ox.setItemElement(1, "ItemName", "Flange");
 		ox.setItemElement(1, "ItemId", "777");
 		ox.setItemElement(1, "Quantity","3");
 		ox.setItemAttribute(1,  "Quantity", "unit", "13");         
		//Get the updated header elements
 		cname = ox.getOrderElement("CustomerName");
 		cid   = ox.getOrderElement("CustomerId");
 		date = ox.getOrderElement("Date");
		//  Get the updated elements for item 1 
 		//	(numbering from 0)      
 		iName1 = ox.getItemElement(1, "ItemName");
 		iId1 = ox.getItemElement(1, "ItemId");
 		iQ1 = ox.getItemElement(1, "Quantity");
 		iU = ox.getItemAttribute(1,  "Quantity", "unit"); 
		System.out.println("\nAFTER UPDATE: ");
 		System.out.println("\n   "+date+ "  "+ cname + " " +cid);
 		System.out.println("\n   "+ iName1+" "+iId1+" "
 			+ iQ1 + " " + iU + "\n");
		//Copy the updated document to another file
 		FileUtil.string2File("Order-updated.xml", ox.getXmlText())
		} catch (Exception e) {
 		System.out.println("Exception:");
 		e.printStackTrace();
 		}
 	}
 }

After implementing the methods in OrderElements, the order document stored in Order-updated.xml is:

<?xml version="1.0"?>
 <!DOCTYPE Order SYSTEM 'Order.dtd'>
 <Order>
 	<Date>1999/07/31</Date>
 	<CustomerId>531</CustomerId>
 	<CustomerName>Best Bakery</CustomerName>
 	<Item>
 		<ItemId> 987</ItemId>
 		<ItemName>Coupler</ItemName>
 		<Quantity>5</Quantity>
 	</Item>
 	<Item>
 		<ItemId>777</ItemId>
 		<ItemName>Flange</ItemName>
 		<Quantity unit="13">3</Quantity>
 	</Item>
 	<Item>
 		<ItemId>579</ItemId
 		<ItemName>Clasp</ItemName
 		<Quantity>1</Quantity>
	 </Item>
 </Order>