[jdom-interest] How to check for whether an element has children.
Benjamin Kopic
ben at kopic.org
Mon Dec 31 04:43:27 PST 2001
you are missing a root element in your XML; it should be something like:
String myXML =
"<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></myXML>";
try this and see if you still get NullPointerException
> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Nott, Matthias
> Sent: 27 December 2001 13:22
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] How to check for whether an element has
> children.
>
>
>
> 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><n
> ame>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
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yourad
dr at yourhost.com
More information about the jdom-interest
mailing list