[jdom-interest] Accessing Child Elements

Peter V. Gadjokov pvg at c-c-s.com
Tue Sep 5 15:07:26 PDT 2000

Element.getChild(String name) assumes you are looking for an element in
the 'empty' namespace and your "result" element belongs to the
xql-prefixed namespace. You get null back and your next chained method
call dies trying to dereference the receiver (null).

This brings an API comment to mind - when namespaces are used, 
getChild(String elemName) becomes quite useless and it's behaviour is
arguably not entirely intuitive. If the implementation were changed to:

public Element getChild(String name)
    return getChild(name, namespace); // use the enclosing namespace

the behaviour in the for the no-namespaces-used remains the same as it
is now and something more sensible is returned when namespaces are in
use.  The pathological case (wanting specifically the empty namespace in
a document with namespace use) is still supported by the more general
api call, the simple call does something that seems more intuitive (to
me, at least). Of course, that still would not have helped Herr
Sonderegger since the root and the immediate child are in different
namespaces but 

result = root.getChild("result", XQL_NAMESPACE);
profile = result.getChild("Profile"); 

would have worked as expected. 


None of this 
-----Original Message-----
From: Rodney Sonderegger
To: jdom-interest at jdom.org
Sent: 9/5/00 11:20 AM
Subject: [jdom-interest] Accessing Child Elements


we're testing JDOM in our application and I found some strange things,
which I can't explain.
I'm using the SAXBuilder to read the Streamdata passed by the Database
  <?xml version="1.0" encoding="utf-8" ?>
  <xql:query>Profile[ProviderKey="0011" and
 <ino:message ino:returnvalue="0">
  <ino:messageline>XQL Request processing</ino:messageline>
 <Profile ino:id="2">
 <ino:cursor ino:count="1">
  <ino:first ino:href="?_XQL(1,16)=Profile[ProviderKey="0011" and
Application="eRetail"]" />
 <ino:message ino:returnvalue="0">
  <ino:messageline>XQL Request processed</ino:messageline>

The Parser works fine and parses the document without errors.
I'm interested in the <Profile> Element and try to get it with:

      Element root = aDoc.getRootElement();
      Element xmlProfile = root.getChild("result").getChild("Profile");

I get an NULL Pointerexception trying to do that.
when I  run an Iterator:

    Iterator anIt = root.getChildren().iterator();
      while (anIt.hasNext()){
        Element aElem = (Element) anIt.next();
        if (aElem.getName().equals("result")){
          xmlProfile = aElem;

then I can access the subelements of <result> does anybody know why this

is the case??

Thanx & Regards
To control your jdom-interest membership:

More information about the jdom-interest mailing list