Java Reference
In-Depth Information
builder = builderFactory.newDocumentBuilder(); // Create the
parser
builder.setErrorHandler(new TryDOM());
// Error
handler is TryDOM instance
} catch(ParserConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
Path xmlFile = Paths.get(args[0]);
Document xmlDoc = null;
try (BufferedInputStream in = new BufferedInputStream(
Files.newInputStream(xmlFile))){
xmlDoc = builder.parse(in);
} catch(SAXException | IOException e) {
e.printStackTrace();
System.exit(1);
}
DocumentType doctype = xmlDoc.getDoctype();
// Get the
DOCTYPE node
if(doctype == null) {
// If it's not
null...
System.out.println("DOCTYPE is null");
} else {
// ...output it
System.out.println("DOCTYPE node:\n" +
doctype.getInternalSubset());
}
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) {
// List the current node
String nodeName = node.getNodeName();
System.out.println(indent + " Node: " + nodeName);
System.out.println(indent + " Node Type: " +
nodeType(node.getNodeType()));
NodeList list = node.getChildNodes();
// Get the list
of child nodes
if(list.getLength() > 0) {
// If there are
some...
//...list them & their children...
// ...by calling listNodes() for each
System.out.println(indent+" Child Nodes of " + nodeName + "
are:");
for(int i = 0 ; i < list.getLength() ; ++i) {
listNodes(list.item(i),indent + " ");
}
Search WWH ::




Custom Search