According to the API at jdom.org, the semantics of getChild(String name):
This returns the first child element within this element with the given local name and belonging to no namespace. If no elements exist for the specified name and namespace, null is returned.
Therefore, if I have an XML structure like:
<?xml version="1.0" encoding="UTF-8"?>
<lvl1>
<lvl2>
<lvl3/>
</lvl2>
</lvl1>
I have a JDOM Element which is currently pointing to <lvl1>. I should be able to make the following call:
Element lvl3 = lvl1Element.getChild("lvl3");
and lvl3 should have non-null.
However, I’m finding that lvl3 is actually null. Am I missing something?
Here is a sample code snippet that should work:
import java.io.StringReader;
import org.jdom.*;
public static void main(String[] args){
Document doc = new SAXBuilder().build(new StringReader("path to file"));
Element lvl1Element = doc.getRootElement();
Element lvl3Element = lvl1Element.getChild("lvl3"); //is null. Why?
}
In order to get the functionality I was looking for, I used an
Iteratorfrom thegetDescendants(ElementFilter)function from jdom.orgI then got the
ElementI was looking for by using code similar to the following: