import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.IOException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree in order to print a document that is parsed.
*
*/
public class TestDOM2 {
protected PrintWriter out;
public TestDOM2() {
out = new PrintWriter(System.out);
}
/** Prints the specified node, recursively. */
public void print(Node node)
{
if ( node == null ) {
return;
}
int type = node.getNodeType();
switch ( type )
{
case Node.DOCUMENT_NODE:
out.println("");
print(((Document)node).getDocumentElement());
out.flush();
break;
case Node.ELEMENT_NODE:
//out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
/*
for ( int i = 0; i < attrs.getLength(); i++ )
{
out.print("attr_value:" + attrs.item(i).getNodeValue());
}
out.println();
*/
NodeList children = node.getChildNodes();
if ( children != null )
{
int len = children.getLength();
for ( int i = 0; i < len; i++ ) {
print(children.item(i));
}
}
break;
case Node.TEXT_NODE:
out.print( node.getNodeValue() );
break;
}
/*
if ( type == Node.ELEMENT_NODE ) {
out.print("elem_node_name:" + node.getNodeName());
out.println(); // HACK
}
*/
out.flush();
}
public static void main(String args[]) {
if ( args.length == 0 ) {
System.exit(1);
}
System.err.println(args[0]);
FileInputStream in;
Tidy tidy = new Tidy();
TestDOM2 t = new TestDOM2();
try {
in = new FileInputStream(args[0]);
tidy.setMakeClean(true);
tidy.setXmlTags(true);
t.print(tidy.parseDOM(in, null));
}
catch ( IOException e ) {
System.err.println( e.toString() );
}
}
}
|