// JAXP APIs qu'on utilise import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; // les exceptions pouvant etre levees import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; // inputs/outputs import java.io.File; import java.io.IOException; // les definitions W3C pour DOM et les exceptions DOM import org.w3c.dom.*; public class SimpleDomParser { static final String[] typeName = { "none", "Element", "Attr", "Text", "CDATA", "EntityRef", "Entity", "ProcInstr", "Comment", "Document", "DocType", "DocFragment", "Notation", }; public static void main(String argv[]) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // // si on veut valider le document par rapport a // sa declaration de type // // factory.setValidating(true); // factory.setNamespaceAware(true); // try { DocumentBuilder builder = factory.newDocumentBuilder(); // C'est ici que se fait le parsing Document document = builder.parse(new File(argv[0])); // Parcours et affichage du document walk_the_tree(document, 0); } catch (SAXException sxe) { // Capture des erreurs de parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Capture des erreurs de configuration pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } } ///////////////////////////////////////////////////////////// public static void walk_the_tree(Node domNode, int depth) { String s = typeName[domNode.getNodeType()]; String nodeName = domNode.getNodeName(); if (! nodeName.startsWith("#")) { s += " " + nodeName; } if (domNode.getNodeValue() != null) { if (! "".equals(domNode.getNodeValue().trim())) { s += " = " + domNode.getNodeValue().trim(); for (int i = 0; i < depth; i++) s = "-" + s; System.out.println(s); } } else { for (int i = 0; i < depth; i++) s = "-" + s; System.out.println(s); NamedNodeMap attrs = domNode.getAttributes(); if (attrs != null) { // un attribut peut contenir des entites .. for (int i = 0; i < attrs.getLength(); i++) walk_the_tree(attrs.item(i), depth+1); } } NodeList child = domNode.getChildNodes(); for (int i = 0; i < child.getLength(); i++) walk_the_tree(child.item(i), depth+1); } }