I need to walk through a JDOM tree and extract all data from body section to use it in another xml document creation. I'm very new to programming. I have attached my concept and error shown in console. I want to clarify whether this concept is right or wrong. Could any body help me to know about this and give a direction?
Would appreciate any pointers..
//root- Existing document's root.
//body- New documents body.
private static Element listChildren(Element root, int depth) {
System.out.println(root.getName());
List children = root.getChild("body").getChildren();
Iterator iterator = children.iterator();
while (iterator.hasNext()) {
Element child = (Element) iterator.next();
System.out.println(child.toString());
body.addContent(child);
listChildren(child, depth+1);
return child;
}
return null;
}
Error shown:
Exception in thread "main" java.lang.NullPointerException
at createXhtml1.listChildren(createXhtml1.java:85)
at createXhtml1.newXhtml(createXhtml1.java:62)
at createXhtml1.main(createXhtml1.java:112)
So based on comments your problem is with
root.getChild("body")
. This method returns null (there's no child named body in root element). You should check for null andreturn null
from method.EDIT According to comment, you can print (or whatever you want to do) all elements.