[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. 

-pvg

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

Hello

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
(Tamino):
-------------------------------------------------------------------
  <?xml version="1.0" encoding="utf-8" ?>
 <ino:response
xmlns:ino="http://namespaces.softwareag.com/tamino/response2"
xmlns:xql="http://metalab.unc.edu/xql/">
  <xql:query>Profile[ProviderKey="0011" and
Application="eRetail"]</xql:query>
 <ino:message ino:returnvalue="0">
  <ino:messageline>XQL Request processing</ino:messageline>
  </ino:message>
 <xql:result>
 <Profile ino:id="2">
  <Application>eRetail</Application>
  <ProviderKey>0011</ProviderKey>
  <DefaultLanguage>de</DefaultLanguage>
  <DefaultPriority>1</DefaultPriority>
  <URLPrefix>www.yellowgate.ch/cgi-bin/ygate/yellowgate</URLPrefix>
  <InfoKeySequence>5</InfoKeySequence>
  </Profile>
  </xql:result>
 <ino:cursor ino:count="1">
  <ino:first ino:href="?_XQL(1,16)=Profile[ProviderKey="0011" and
Application="eRetail"]" />
  </ino:cursor>
 <ino:message ino:returnvalue="0">
  <ino:messageline>XQL Request processed</ino:messageline>
  </ino:message>
  </ino:response>
----------------------------


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
Rodney
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@you
rhost.com



More information about the jdom-interest mailing list