Actually I have an XML document. I would like to print the children of the root element of the document using a utility in org.w3c.Dom without printing the document headers.
So I need a utility in org.w3c.Dom to print a Node only. any help please ?
Actually I have an XML document. I would like to print the children of the root element of the document using a utility in org.w3c.Dom without printing the document headers.
So I need a utility in org.w3c.Dom to print a Node only. any help please ?
Assuming you have an org.w3c.dom.Document you can print the names of the child nodes of the root element with something like:
// assuming: Document doc = ...;
NodeList childNodes = doc.getDocumentElement().getChildNodes();
for(int i = 0; i < childNodes.getLength(); i++){
System.out.println(childNodes.item(i).getNodeName());
}
Realistically, if you want to navigate the DOM using the org.w3c.dom utilities you'll need to figure out which API calls are most relevant for you.
An excellent starting point is the API documentation (that I referred to to answer your question). Here's the relevant pages for this particular answer:
I have an XML utility class that I use for simple DOM functionality. In this case you would be able to use the XmlUtil.writeXml(Node node, Writer writer) to write individual subtrees of a document.
The source code is available here