[jdom-interest] How to check for whether an element has child ren.

Nott, Matthias mnott at vignette.com
Thu Dec 27 06:55:17 PST 2001


Ok all, thank y'all for the response; it was quite obvious, so
sorry for having bothered you. Perhaps someone can help with a
more general thing I want to achieve.

I have a recursive xml string like 

<obj>
  <oid>100</oid>
  <jsp>page.jsp</jsp>
  <prpx>valx</prpx>
  
  <obj>
    <oid>110</oid>
    <jsp>some.jsp</jsp>
    <prpy>valy</prpy>
  </obj>

  <obj>
    <oid>110</oid>
    <jsp>some.jsp</jsp>
    <prpy>valy</prpy>
  </obj>
</obj>

Every obj node has an element jsp - that  contains the name of 
a jsp which will display the node  by using some of  the other 
xml elements of that node, and at some point  in time  in turn 
find out it has children and call their <jsp>.

At the root node, I retrieve the xml string from the database.

As far as I understand, it would be  best having a taglib that
implements tags like <lib:get var="prpy">  for retrieving  the
element values of any given element, and to have some iterator
like <lib:iterate> to iterate over all children of the current
object.

Yet, I do not know how to most efficiently handle the xml data
- I do not want to parse again and again for each object, only
to find out the current node. I want to hold the xml tree, on-
ce it was parsed, somewhere and just move a pointer across the
tree pointing to the current node, so that any given object is
able to get its information and to call its children.

This information will probably be held with session scope, and
use jsp:include to include one <jsp> after the other.

Any help would be greatly appreciated.

Thanks

M




-----Original Message-----
From: Ian Lea [mailto:ian.lea at blackwell.co.uk]
Sent: Thursday, December 27, 2001 3:03 PM
To: Nott, Matthias
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] How to check for whether an element has
children.


As you say c does have children, "oid" and "name", but c doesn't
have a child "object" hence n = c.getChild("object") returns null,
and s = n.hasChildren throws NPE.  I think you either need to
check for n == null or use the List methods.

--
Ian.
ian.lea at blackwell.co.uk


"Nott, Matthias" wrote:
> 
> Y'all,
> 
> Sorry for the newbie question, but why does the following
> code result in a null pointer?
> 
>         String myXML =
> 
>
"<object><oid>4711</oid><name>page</name><object><oid>4712</oid><name>child1
>
</name></object><object><oid>4713</oid><name>child2</name></object></object>
> ";
> 
>         Document tXML = new SAXBuilder().build(new StringReader(myXML));
>         Element e = tXML.getRootElement();
>         Element c = e.getChild("object");
>         Element n = c.getChild("object");
>         boolean s = n.hasChildren();
> 
>         out.println("<xmp>"+s+"</xmp>");
> 
> If I say, s = c.hasChildren(); I get true, as this is object oid 4712
which
> has children oid and name. I would have expected to s = n.hasChildren() to
> return false, but not a null pointer. The same error appears if I try to
> String s = n.getChildren().toString();
> 
> So how can I check without getting an exception for whether an element
> still has children? I can convert it to a list and then check out the
> index for the <object> element, but I am a bit afraid I might be loosing
> performance here.
> 
> Thanks
> 
> Matthias



More information about the jdom-interest mailing list