Saturday, February 19, 2011

JDOM - getChild() Or getChildText() Returns null

If you're using JDOM for parsing XML in Java and you run into the problem that Element#getChild(String) and Element#getChildText(String) just keep returning null, here's a list of reasons why that might be happening:
  1. The node really doesn't exist!
  2. The node is in a different namespace and you need to use the version of the method that also accepts the namespace: Element#getChild(String, Namespace) or Element#getChildText(String, Namespace).
  3. Most importantly, remember that these methods return only immediate descendents of a node! So if you're looking to get a node nested at the nth level, you will need to walk the tree to its parent and only then can you call #getChild() and actually get the child!

1 comment:

Anonymous said...

That sucks doesn't it? i was hoping jdom would automatically find and return the sub-element