5.4.3 Sample program (SampleDOM.java)
The following shows the sample program:
import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.xml.sax.SAXException; import org.w3c.dom.*; public class SampleDOM { public static final void main(String[] args) { try{ new SampleDOM().CreateXMLFile(); new SampleDOM().GetStringFromXML(); }catch(Exception exception){ exception.printStackTrace(); } } //create XML file public final void CreateXMLFile() { try{ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element parent = doc.createElement("Payment_Slip"); Element sub = doc.createElement("Merchandise"); Element leaf1 = doc.createElement("Brand_Name"); Element leaf2 = doc.createElement("Unit_Price"); Element leaf3 = doc.createElement("Amount"); leaf1.appendChild(doc.createTextNode("Television")); sub.appendChild(leaf1); leaf2.appendChild(doc.createTextNode("15000")); sub.appendChild(leaf2); leaf3.appendChild(doc.createTextNode("14")); sub.appendChild(leaf3); parent.appendChild(sub); sub = doc.createElement("Merchandise"); leaf1 = doc.createElement("Brand_Name"); leaf2 = doc.createElement("Unit_Price"); leaf3 = doc.createElement("Amount"); leaf1.appendChild(doc.createTextNode("Radio")); sub.appendChild(leaf1); leaf2.appendChild(doc.createTextNode("3500")); sub.appendChild(leaf2); leaf3.appendChild(doc.createTextNode("6")); sub.appendChild(leaf3); parent.appendChild(sub); sub = doc.createElement("Merchandise"); leaf1 = doc.createElement("Brand_Name"); leaf2 = doc.createElement("Unit_Price"); leaf3 = doc.createElement("Amount"); leaf1.appendChild(doc.createTextNode("Video")); sub.appendChild(leaf1); leaf2.appendChild(doc.createTextNode("21000")); sub.appendChild(leaf2); leaf3.appendChild(doc.createTextNode("4")); sub.appendChild(leaf3); parent.appendChild(sub); doc.appendChild(parent); OutputStream os = new BufferedOutputStream( new FileOutputStream("SampleDOM.xml")); TransformerFactory tf = TransformerFactory.newInstance(); Transformer tran = tf.newTransformer(); tran.setOutputProperty("encoding", "Shift_JIS"); tran.setOutputProperty("indent", "yes"); tran.transform(new DOMSource(doc), new StreamResult(os)); os.flush(); os.close(); }catch(ParserConfigurationException e){ e.printStackTrace(); }catch(TransformerConfigurationException e){ e.printStackTrace(); }catch(TransformerException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } //acquire data from the XML file made by CreateXMLFile, and calculate public final void GetStringFromXML() { try{ int tvPrice = 0; int radioPrice = 0; int videoPrice = 0; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("SampleDOM.xml"); Element element1 = doc.getDocumentElement(); NodeList nodelist1 = element1.getElementsByTagName("Merchandise"); //loop for number of the merchandise for(int nShouhin = 0 ; nShouhin < nodelist1.getLength() ; nShouhin++){ String str = new java.lang.String(); int price = 0; int number = 0; Element element2 = (Element)nodelist1.item(nShouhin); //get the brand name NodeList nodelist2 = element2.getElementsByTagName("Brand_Name"); Node node1 = nodelist2.item(0); if(node1.getNodeType() == Node.ELEMENT_NODE){ Element element3 = (Element)node1; NodeList nodelist3 = element3.getChildNodes(); Node node2 = nodelist3.item(0); str = node2.getNodeValue(); } //get the unit price of a brand name nodelist2 = element2.getElementsByTagName("Unit_Price"); node1 = nodelist2.item(0); if(node1.getNodeType() == Node.ELEMENT_NODE){ Element element3 = (Element)node1; NodeList nodelist3 = element3.getChildNodes(); Node node2 = nodelist3.item(0); price = Integer.parseInt(node2.getNodeValue()); } //get the amount of a brand name nodelist2 = element2.getElementsByTagName("Amount"); node1 = nodelist2.item(0); if(node1.getNodeType() == Node.ELEMENT_NODE){ Element element3 = (Element)node1; NodeList nodelist3 = element3.getChildNodes(); Node node2 = nodelist3.item(0); number = Integer.parseInt(node2.getNodeValue()); } //subtotal calculation of each merchandise if(str.equals("Television")){ tvPrice = price * number; }else if(str.equals("Radio")){ radioPrice = price * number; }else if(str.equals("Video")){ videoPrice = price * number; } } int sum = tvPrice + radioPrice + videoPrice; System.out.println(sum + " yen in total."); }catch(ParserConfigurationException e){ e.printStackTrace(); }catch(SAXException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }