[jdom-interest] Bug and query

Eric VERGNAUD eric.vergnaud at wanadoo.fr
Wed Jun 25 02:29:16 PDT 2003

Thanks Bradley for your feedback,

>>     in XmlOutputter.java, there is come inconsistency in the way
>>     printElement, printElementNamespace, and printNamespace work together.
>>     This sometimes leads to the following production:
>>     <tag xmlns:'mynamesapce'>
>>         <childtag xmlns:>
>>         some data
>>         </childtag>
>>     </tag>
>>     This happens when you set the namespace for an element, but not for one
>> of its child element.
>>     This can be fixed by changing the code for printElementNamespace by the
>> following:
>>         Namespace ns = element.getNamespace();
>>         if (ns != Namespace.XML_NAMESPACE && ns != Namespace.NO_NAMESPACE)
>>         {
>>             printNamespace(ns, out, namespaces);
>>         }
> Which version of JDOM are you using? In beta 9 and the current cvs, ns should
> should never be equal to XML_NAMESPACE (which is prefix "xml").  Using the
> prefix "xml" or one that starts with "xml" will throw a exception in
> Namespace.getNamespace. Second "xmlns" is only printed in printNamespace()
> and it always prints a "=" and surrounds the URI with double quotes, so I
> don't see how your example output could happen.

The production is more like the following (sorry for the lack of precision
in my previous post):

     <tag xmlns="mynamespace">
         <childtag xmlns="">
            some data

I don't know whether xmlns="" is legal, but it's not supported by any parser
I've used. I'm still using b8, but I've checked that this code remains
unchanged in b9. In my case, the problem occurs when ns is
Namespace.NO_NAMESPACE. This is because the current code in
printElementNamespace() calls printNamespace() when either ns is not
Namespace.NO_NAMESPACE OR the namespaces stack is not empty:

        if ( !((ns == Namespace.NO_NAMESPACE) &&
               (namespaces.getURI("") == null))) {
            printNamespace(ns, out, namespaces);

but printNamespace prints an empty namespace when ns is

>>  in XmlOutputter.java, the 'output' method ends by the following code:
>>     // Output final line separator
>>     // We output this no matter what the newline flags say
>>     out.write(currentFormat.lineSeparator);
>>     out.flush();
>> Is writing a line separator at the end of the Xml stream mandatory ? I don't
>> know the answer, but I guess it's no, so I think it should be caller's
>> responsability to add this newline, because while this newline maybe fine in
>> a xml file, it may not be fine at all in a wireless xml stream, when the
>> other end is expecting a perfectly-formed (not just well-formed) document.
>> In fact I've met the situation where this newline would cause the other end
>> parser to fail at some point, and got stuck because I do not have access to
>> that parser which resides compiled on a mobile device.
> No it's not mandatory, but it should not matter. If it does, then that
> application or the parser it uses it broken.
>> So I would very much prefer:
>>     // Output final line separator
>>     // We output this no matter what the newline flags say
>>     newline(out);
>>     out.flush();
>> What do you think ?
> Have to look, but not printing it when in "raw" mode and printing it
> when in "pretty-printing" mode seems harmless and reasonable.

Thanks, because there is absolutely no way I can fix the bug on the mobile
device's parser. What happens apparently is that because the mobile device
has limited memory, it parses the xml stream on the fly without waiting for
the root closing tag. I guess the -buggy- mechanism which keeps any unparsed
data in the buffer over calls to the receiving socket, leads to a situation
where the 2 line-break characters at the end of the first received document
(which are not parsed), become the 2 leading characters of the second
document (which is not legal).

More information about the jdom-interest mailing list