[jdom-interest] Problem with getChild()

Galluzzo, Eric EGalluzzo at synchrony.net
Tue Sep 26 06:05:48 PDT 2000


> -----Original Message-----
> From: Jorge Acebo [mailto:jacebo at essi.es]
> 
> Hello.
> 
> I have an xml document that begins like this:
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <!DOCTYPE record SYSTEM "IMS_METADATAv1p1.dtd">
> <record xmlns="http://www.imsproject.org/metadata/">
>  <metametadata>
>       <catalogentry>
>            <catalogue></catalogue>
>       </catalogentry>
>   </metametadata>
> <general>
>     .... etc...
> 
> and I have the next code to read the children of <record> element:
> 
> Element root = doc.getRootElement();
> List a = root.getChildren();
> Iterator i = a.iterator();
> while ( i.hasNext() )
> {
>     Element e = (Element) i.next();
>     System.out.println("Children: " + e.getName());
> }
> 
> This works well, and list "metadata", "general", etc... But when I try
> to acces to one child:
> 
>    Element e = root.getChild("metametadata");
> 
> 
> getChild returns a null pointer.
> 
> Why?

Because getChild(String) will only retrieve children in the empty namespace,
not in the parent element's namespace.  You want getChild(String,
Namespace), which will get children in the given namespace (i.e.
Namespace.getNamespace ("http://www.imsproject.org/metadata/")).  This
stumped me too when I started using JDOM. :)  There has been discussion as
to whether this should be changed, but no real decision has been made.

    - Eric



More information about the jdom-interest mailing list