[jdom-interest] Beta 9 RC1 (tiny namespace bug)

Jason Hunter jhunter at servlets.com
Fri Apr 11 14:03:34 PDT 2003


> When creating JDOM documents, one element at a time
> in memory, we sometimes desire to have the following effect:
> 
>         <mytag> </mytag>
> 
> instead of the abbreviation:
> 
>         <mytag/>
> 
> That is, I desire an open and close tag with no text in between.
> 
> This is required, so that Javascript can add children to that node later,
> in SVG graphics documents that are modified dynamically on the web.
> 
> One cannot achieve this effect by putting text composed of blanks,
> non-breaking spaces (in either hex or html format), etc.

With the default settings of XMLOutputter the only way you should see
<mytag/> is if there's absolutely no content to the element.  One space
will become <mytag> </mytag>.  If it doesn't, it's a bug.  I suspect you
turned on text trimming, so of course whitespace gets trimmed.

> But one can achieve it, in JDOM b8, with the following command:
> 
>         Element text = new Element("text");
>         text.setAttribute("xml:space", "preserve");
> 
> JDOM b8 did not require a namespace to be set for this to work.
> In JDOM b9 the workaround is to use the following
> 
>         Namespace xml = Namespace.getNamespace("xml",
>                 "http://www.w3.org/XML/1998/namespace");
>         Element text = new Element("text");
>         text.setAttribute("space", "preserve", xml);
> 
> THIS IS A BUG, BECAUSE the xml namespace is always
> supposed to be available in all xml tools.

It's not actually a bug, as was pointed out earlier.  Just pass in the
pre-existing Namespace.XML_NAMESPACE as the namespace.

> But in JDOM b9, the following error message occurs, if the above b8 code is used under b9:
> 
> org.jdom.IllegalNameException:
> The name "xml:space" is not legal for JDOM/XML attributes:
> Attribute names cannot contain colons.

Yep, to put an attribute in a namespace, pass in the namespace.

-jh-



More information about the jdom-interest mailing list