Java Reference
In-Depth Information
import java.io.IOException;
public class TryDOM implements ErrorHandler {
public static void main(String args[]) {
if(args.length == 0) {
System.out.println("No file to process."+
"Usage is:\njava TryDOM \"filename\"");
System.exit(1);
}
File xmlFile = new File(args[0]);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true); // Set namespace aware
builderFactory.setValidating(true); // and validating parser feaures
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder(); // Create the parser
builder.setErrorHandler(new TryDOM()); //Error handler is instance of TryDOM
} catch(ParserConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(xmlFile);
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
DocumentType doctype = xmlDoc.getDoctype(); // Get the DOCTYPE node
System.out.println("DOCTYPE node:\n" + doctype); // and output it
System.out.println("\nDocument body contents are:");
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
}
// output a node and all its child nodes
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent+nodeName+" Node, type is "
+node.getClass().getName()+":");
System.out.println(indent+" "+node);
NodeList list = node.getChildNodes(); // Get the list of child nodes
if(list.getLength() > 0) { // As long as there are some...
System.out.println(indent+"Child Nodes of "+nodeName+" are:");
for(int i = 0 ; i<list.getLength() ; i++) //...list them & their children...
listNodes(list.item(i),indent+" "); // by calling listNodes() for each
Search WWH ::




Custom Search