/* Parser skeleton for processing item-???.xml files. Must be compiled in * JDK 1.5 or above. * * Instructions: * * This program processes all files passed on the command line (to parse * an entire diectory, type "java MyParser myFiles/*.xml" at the shell). * */ import java.io.*; import java.text.*; import java.util.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.w3c.dom.Text; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.ErrorHandler; class MyDOM { static final String columnSeparator = "|*|"; static DocumentBuilder builder; static final String[] typeName = { "none", "Element", "Attr", "Text", "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment", "Document", "DocType", "DocFragment", "Notation", }; static class MyErrorHandler implements ErrorHandler { public void warning(SAXParseException exception) throws SAXException { fatalError(exception); } public void error(SAXParseException exception) throws SAXException { fatalError(exception); } public void fatalError(SAXParseException exception) throws SAXException { exception.printStackTrace(); System.out.println("There should be no errors " + "in the supplied XML files."); System.exit(3); } } /* Returns the amount (in XXXXX.xx format) denoted by a money-string * like $3,453.23. Returns the input if the input is an empty string. */ static String strip(String money) { if (money.equals("")) return money; else { double am = 0.0; NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); try { am = nf.parse(money).doubleValue(); } catch (ParseException e) { System.out.println("This method should work for all " + "money values you find in our data."); System.exit(20); } nf.setGroupingUsed(false); return nf.format(am).substring(1); } } /* Process one items-???.xml file. */ static void processFile(File xmlFile) { Document doc = null; try { doc = builder.parse(xmlFile); } catch (IOException e) { e.printStackTrace(); System.exit(3); } catch (SAXException e) { System.out.println("Parsing error on file " + xmlFile); System.out.println(" (not supposed to happen with supplied XML files)"); e.printStackTrace(); System.exit(3); } /* At this point 'doc' contains a DOM representation of an 'Items' XML * file. Use doc.getDocumentElement() to get the root Element. */ System.out.println("Successfully parsed - " + xmlFile); /* Fill in code here (you will probably need to write auxiliary methods). */ /**************************************************************/ recursiveDescent(doc, 0); } public static void recursiveDescent(Node n, int level) { // adjust indentation according to level for(int i=0; i<4*level; i++) System.out.print(" "); // dump out node name, type, and value String ntype = typeName[n.getNodeType()]; String nname = n.getNodeName(); String nvalue = n.getNodeValue(); System.out.println("Type = " + ntype + ", Name = " + nname + ", Value = " + nvalue); // dump out attributes if any org.w3c.dom.NamedNodeMap nattrib = n.getAttributes(); if(nattrib != null && nattrib.getLength() > 0) for(int i=0; i