/* a dom parser that displays attibutes and text */ import javax.xml.parsers.*; import org.xml.sax.*; import java.io.*; import org.w3c.dom.*; import java.lang.String; public class DomParser2{ public static void main(String argv[]) { if (argv.length != 1) { System.err.println("Usage: java DomParser2 filename"); System.exit(1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // factory.setValidating(true); // factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse( new File(argv[0]) ); Element root = document.getDocumentElement(); if (root != null) PrintTree(root,0); } catch (SAXException sxe) { // Error generated during parsing) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } } // main // PrintTree - prints the dom tree, indenting each level // an additional 4 spaces. it also prints the attributes // and the embedded text for para and chaptertitle private static void PrintTree(Node n, int indent) { Element e = (Element)n; String s = e.getTagName(); int i; for (i=0;i