Here you can find the source of printDOMTree(Node node, PrintStream out)
Parameter | Description |
---|---|
node | starting Node |
out | where to write the result, for example System.out |
public static void printDOMTree(Node node, PrintStream out)
//package com.java2s; import java.io.PrintStream; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /**/*from ww w . jav a2s. c o m*/ * For debug purpose. Returns a graphical representation of a given DOM Tree * starting at node. * * @param node * starting Node * @param out * where to write the result, for example System.out */ public static void printDOMTree(Node node, PrintStream out) { int type = node.getNodeType(); switch (type) { // print the document element case Node.DOCUMENT_NODE: { printDOMTree(((Document) node).getDocumentElement(), out); break; } // print element with attributes case Node.ELEMENT_NODE: { out.print("<"); out.print(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } out.print(">"); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) printDOMTree(children.item(i), out); } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { out.print("&"); out.print(node.getNodeName()); out.print(";"); break; } // print cdata sections case Node.CDATA_SECTION_NODE: { out.print("<![CDATA["); out.print(node.getNodeValue()); out.print("]]>"); break; } // print text case Node.TEXT_NODE: { out.print(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String data = node.getNodeValue(); { out.print(" "); out.print(data); } out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { out.print("</"); out.print(node.getNodeName()); out.print('>'); } } }