you don't need include any .jar if you are using jdk1.4
import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element;import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class OperationXMLFile extends Object{ public static void main(String[] args) throws Exception{ OperationXMLFile.createXMLDoc(); }
public static void createXMLDoc() throws Exception{
String ordersFile = "orders.xml";
//Create the XML document Document ordersDoc = null; try { //Instantiate the parser and parse the file DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder(); ordersDoc = docbuilder.parse(ordersFile);
Element ordersRoot = ordersDoc.getDocumentElement(); NodeList orders = ordersRoot.getElementsByTagName("order"); for (int i = 0; i < orders.getLength(); i++) { //For each order, get the order element Element thisOrder = (Element)orders.item(i);
//Get customer information String thisCustomerid = thisOrder.getElementsByTagName("customerid") .item(0) .getFirstChild().getNodeValue(); System.out.println("Customer: " + thisCustomerid); //Loop through each product for the order NodeList products = thisOrder.getElementsByTagName("product"); for (int j=0; j < products.getLength(); j++) {
Element thisProduct = (Element)products.item(j); //Get product information from attributes and child //elements String thisProductid = thisProduct.getAttribute("productid"); System.out.println(" Product: " + thisProductid);
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class OperationXMLFile extends Object{ public static void main(String[] args) throws Exception{
OperationXMLFile.createXMLDoc();
}
public static void createXMLDoc() throws Exception{
String ordersFile = "orders.xml";
//Create the XML document
Document ordersDoc = null;
try {
//Instantiate the parser and parse the file
DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
ordersDoc = docbuilder.parse(ordersFile);
Element ordersRoot = ordersDoc.getDocumentElement();
NodeList orders = ordersRoot.getElementsByTagName("order");
for (int i = 0; i < orders.getLength(); i++) {
//For each order, get the order element
Element thisOrder = (Element)orders.item(i);
String thisOrderid = thisOrder.getAttribute("orderid");
System.out.println("Order: " + thisOrderid);
//Get customer information
String thisCustomerid = thisOrder.getElementsByTagName("customerid")
.item(0)
.getFirstChild().getNodeValue();
System.out.println("Customer: " + thisCustomerid); //Loop through each product for the order
NodeList products = thisOrder.getElementsByTagName("product");
for (int j=0; j < products.getLength(); j++) {
Element thisProduct = (Element)products.item(j); //Get product information from attributes and child
//elements
String thisProductid = thisProduct.getAttribute("productid");
System.out.println(" Product: " + thisProductid);
String priceStr = thisProduct.getElementsByTagName("price")
.item(0)
.getFirstChild().getNodeValue();
System.out.println(" Price: " + priceStr); String qtyStr = thisProduct.getElementsByTagName("qty")
.item(0)
.getFirstChild().getNodeValue();
System.out.println(" Quantity: " + qtyStr);
System.out.println();
}
}
} catch (Exception e) {
System.out.println("Cannot read the orders file: "+e.getMessage());
}
}
}