[jdom-interest] getChild and namespace

philip.nelson at omniresources.com philip.nelson at omniresources.com
Tue Mar 27 21:10:16 PST 2001

OK I can parse *some* of the adobe examples.

> If you have a default namespace (usually the name of the dtd 
> without the
> .dtd extension), and there are other namespaces declared in 
> the root element
> of your document....
> <?xml version="1.0" encoding="iso-8859-1"?>
> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN"
> "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303
> -stylable.dtd"
> >
> <svg  xmlns:a="http://www.adobe.com/svg10-extensions"
> a:timeline="independent">
>   <defs>
>     <symbol id="rectangle" >
>     <path  d="M0,0L0,50L50,50L0,0z" />
>     </symbol>
>   </defs>
>   <g>
>     <use xlink:href="#rectangle" />
>   </g>
> </svg>
> If you do a doc.getRootElement(), and then get the Namespace 
> it is the path
> to the dtd without the dtd extension. 

[Namespace: prefix "" is mapped to URI "
http://www.w3.org/2000/svg-20000303-stylable "]

The spaces are a strange carryover from the fact that the namespace is
declared in the dtd as an entity.  It caused problems with some of the other
Adobe examples but not here.  The space should have blown up but the check
is not in Verifier yet and if it was in there, none of the Adobe examples
will work without doing a trim before setting the uri.

By the way, the xlink:href was not declared anywhere in this example and I
had to cut it out.

> If you do a 
> root.getChild("defs"), the
> return is null, but if you do 

defs is null: true

> root.getChild("defs",root.getNamespace()), you
> get the symbol element.

nope. The defs element is returned (whew!)

defs namespace: [Namespace: prefix "" is mapped to URI "
http://www.w3.org/2000/svg-20000303-stylable "]
defs name: defs

Here is the code I used;

XMLOutputter out = new XMLOutputter("  ", true);

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build("file:///e:/temp/test.xml");
out.output(doc, System.out);

Element root = doc.getRootElement();

Element defs = root.getChild("defs");

System.out.println("defs is null: " + (defs == null));
defs = root.getChild("defs", root.getNamespace());
System.out.println("defs namespace: " + defs.getNamespace());
System.out.println("defs name: " + defs.getName());

> I am sorry, but how can that be a practical implementation? 
> Its not very
> polymorthic. That is, jdom code written for xml with no 
> namespace's should
> work the same as code written for the default namespace. It makes the
> getChild("name") method almost useless, because any minor 
> change in the xml
> document over time will almost certainly include new 
> namespaces, but the
> presense of a default namespace will not change.

I guess I can see your point.  It would be a change that could break a lot
of code. It was heavily discussed in September 2000 so if you want to take
up the fight, you should read those posts by Elliote Rusty Harold and Peter
Gadjokov, subject "Accessing Child Elements".  However, I don't see how, if
you have used getChild(name, parent.getNamespace()) you would be at any more
risk later if your childrens namespaces changed.  They would still not match
the parents default namespace, if it existed.  What you suggest is a
convienience but not a huge one.

More information about the jdom-interest mailing list