Here you can find the source of printDocument(Document d)
Parameter | Description |
---|---|
d | the specified document. |
private static void printDocument(Document d)
//package com.java2s; /*// ww w .jav a2 s . c om * $RCSfile: XMLUtil.java,v $ $Revision: 1.5 $ $Date: 2007/11/27 02:27:42 $ * $AIST_Release: 5.0.0 $ * $AIST_Copyright: * Copyright 2003, 2004, 2005, 2006 Grid Technology Research Center, * National Institute of Advanced Industrial Science and Technology * Copyright 2003, 2004, 2005, 2006 National Institute of Informatics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * $ */ import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Prints the specified document. * * @param d the specified document. */ private static void printDocument(Document d) { printNode(d.getDocumentElement(), 0); } /** * Prints information of the specified node. * * @param node the specified node. * @param level a number of indent. */ public static void printNode(Node node, int level) { if (node == null) return; switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: printAttribute(node, level); break; case Node.TEXT_NODE: printTextNode(node, level); break; default: printElement(node, level); } } /** * Prints attribute variables of the specified node. * * @param node the specified node. * @param level a number of indent. */ private static void printAttribute(Node node, int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println("<" + node.getNodeName() + " = " + node.getNodeValue() + ">"); } /** * Prints variables of TEXT nodes. * * @param node a parent node. * @param level a number of indent. */ private static void printTextNode(Node node, int level) { String tmp = node.getNodeValue(); tmp = tmp.trim(); if (tmp.length() == 0) { return; } for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.println("[" + tmp + "]"); } /** * Prints elements of the specified node. * * @param node the specified node. * @param level a number of indent. */ private static void printElement(Node node, int level) { for (int i = 0; i < level; i++) { System.out.print(" "); } System.out.print("{" + node.getNamespaceURI() + "}"); System.out.println(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { printNode(attrs.item(i), level + 1); } } NodeList children = node.getChildNodes(); int n = children.getLength(); for (int i = 0; i < n; i++) { Node child = children.item(i); printNode(child, level + 1); } } /** * Gets variable of the specified node. * * @param node the specified node. * @return variable of the specified node. */ public static String getNodeValue(Node node) { return node.getNodeValue(); } }