Here you can find the source of printNode(Node aNode, int aLevel)
Parameter | Description |
---|---|
aNode | parent node of tree to be printed |
aLevel | indicates the current indentation level |
public static void printNode(Node aNode, int aLevel)
//package com.java2s; /*/* www. jav a 2 s . c om*/ * Copyright (c) 2010 The Regents of the University of California. * All rights reserved. * * '$Author: welker $' * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $' * '$Revision: 24234 $' * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the above * copyright notice and the following two paragraphs appear in all copies * of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * ENHANCEMENTS, OR MODIFICATIONS. * */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Prints a DOM Tree (recursive) * * @param aNode * parent node of tree to be printed * @param aLevel * indicates the current indentation level */ public static void printNode(Node aNode, int aLevel) { if (aNode == null) { return; } String spaces = ""; for (int i = 0; i < aLevel; i++) { spaces += ".."; } System.out.println(spaces + "Name: " + aNode.getNodeName()); System.out.println(spaces + "Type: " + aNode.getNodeType()); System.out.println(spaces + "Value: " + aNode.getNodeValue()); NodeList list = aNode.getChildNodes(); if (list != null) { for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); printNode(child, aLevel + 1); } } } /** * ------------------------------------------------------------ Gets the * String value of a node. First checks it's value and if that is null then * it checks to see if it has a child node and gets the value of the first * child. Assumption: That the first child is a #text node, delibertly NOT * checking the first node's type * * @param aNode * Parent to search (should be the document root) * @return Returns the value of the node * -------------------------------------------------------------- */ public static String getNodeValue(Node aNode) { String value = null; if (aNode.getNodeValue() != null) { value = aNode.getNodeValue() != null ? aNode.getNodeValue() .trim() : null; } else { NodeList list = aNode.getChildNodes(); if (list.getLength() == 1) { Node child = list.item(0); if (child != null) { value = child.getNodeValue() != null ? child .getNodeValue().trim() : null; } } } return value; } }