domwalker.java

来自「开源框架」· Java 代码 · 共 55 行

JAVA
55
字号
/**
 */

package org.infohazard.domify.test;

import org.w3c.dom.*;
import java.io.*;

/**
 */
public class DOMWalker
{
	PrintStream out;
	int depth = 0;

	public DOMWalker(PrintStream out)
	{
		this.out = out;
	}

	public void walk(Node node)
	{
		String nodeName;
		try { nodeName = node.getNodeName(); }
		catch (UnsupportedOperationException ex) { nodeName = "Unsupported"; }

		String nodeValue;
		try { nodeValue = node.getNodeValue(); }
		catch (UnsupportedOperationException ex) { nodeValue = "Unsupported"; }

		println("node type:   " + node.getNodeType());
		println("node name:   " + nodeName);
		println("node value:  " + nodeValue);

		depth++;

		NodeList children = node.getChildNodes();
		for (int i=0; i<children.getLength(); i++)
		{
			Node child = children.item(i);
			this.walk(child);
		}

		depth--;
	}

	protected void println(String str)
	{
		for (int i=0; i<depth; i++)
			out.print("  ");

		out.println(str);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?