/* another dom parsery */ import javax.xml.parsers.*; import org.xml.sax.*; import java.io.*; import org.w3c.dom.*; import java.lang.String; public class DomParser3{ public static void main(String argv[]) { if (argv.length != 1) { System.err.println("Usage: java DomParser filename"); System.exit(1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse( new File(argv[0]) ); // this could also read from a URL or any other stream Element root = document.getDocumentElement(); if (root != null) PrintTree(root); } catch (SAXException sxe) { // Error generated during parsing System.err.println(sxe); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built System.err.println(pce); } catch (IOException ioe) { ioe.printStackTrace(); } } // end of main private static void PrintTree(Element e) { int i; Node n,child; NodeList children; NamedNodeMap attributes; attributes = e.getAttributes(); System.out.println("Element " + e.getNodeName() + " has " + attributes.getLength() + " attributes"); for(i=0;i