// 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 SimpleDomParserIndent { 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(document, -1); } 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(); } catch ( Exception ex ) { ex.printStackTrace(); } } ///////////////////////////////////////////////////////////// public static void walk(Node domNode, int depth) { int type = domNode.getNodeType(); String nodeName = domNode.getNodeName(); String nodeValue = domNode.getNodeValue(); String output = ""; // Element if (type == 1) { shift(depth); output = "<"+ nodeName; System.out.print(output); NamedNodeMap attrs = domNode.getAttributes(); if (attrs != null) { // un attribut peut contenir des entites .. for (int i = 0; i < attrs.getLength(); i++) walk(attrs.item(i), depth+1); } //Fin du debut de l'element System.out.println(">"); // Parcours des fils (non attributs) NodeList child = domNode.getChildNodes(); for (int i = 0; i < child.getLength(); i++) walk(child.item(i), depth+1); // Balise fermante shift(depth); output = ""; System.out.println(output); } // Attribute else if (type == 2) { output = " " + nodeName + "=" + "\"" + nodeValue + "\""; System.out.print(output); } // Text else if (type == 3) { output = nodeValue.trim(); if (!"".equals(output)) { shift(depth); output += "\n"; System.out.print(output); } } // Document else if (type == 9) { output = ""; System.out.println(output); // Parcours des fils (non attributs) // Pour le noeud Document, il n'y a pas d'attributs NodeList child = domNode.getChildNodes(); // Pour ce noeud, inutile d'augmenter l'indentation for (int i = 0; i < child.getLength(); i++) walk(child.item(i), depth+1); } // Autres noeuds else { // Parcours des fils (non attributs) NodeList child = domNode.getChildNodes(); for (int i = 0; i < child.getLength(); i++) walk(child.item(i), depth+1); } } public static void shift(int depth) { for (int i = 0; i