[jdom-interest] Small but irritating problem

Jason Hunter jhunter at xquery.com
Tue Oct 26 09:32:46 PDT 2004


Make the remove call after the transform.

-jh-

Jan-Olof Hendig wrote:

> Hello Jason,
> 
> Thank you for your reply (and to the others that replied as well),
> 
> I tried your workaround, putting it as the first line in 
> documentToString method, i.e.
> 
> public static String documentToString(Document doc, boolean fIndent) {
>    doc.getRootElement().removeNamespaceDeclaration(Namespace.NO_NAMESPACE);
>    JDOMSource domSource = new JDOMSource(doc);
>    StringWriter stringWriter = new StringWriter();
> 
> This does not work for me, the 'xmlns' is still there. Perhaps I've 
> misunderstood how to apply this workaround. Should I place the line 
> somewhere else?
> 
> Best regards,
> 
> Jan-Olof Hendig
> 
> ----- Original Message ----- From: "Jason Hunter" <jhunter at xquery.com>
> To: "Bradley S. Huffman" <hip at cs.okstate.edu>
> Cc: "Jan-Olof Hendig" <jan-olof.hendig at bredband.net>; 
> <jdom-interest at jdom.org>
> Sent: Tuesday, October 26, 2004 3:36 AM
> Subject: Re: [jdom-interest] Small but irritating problem
> 
> 
>> Bummer.  I'll add it to the list of things to fix.  In the meanwhile, 
>> you can work around it by calling 
>> doc.getRootElement().removeNamespaceDeclaration(Namespace.NO_NAMESPACE).
>>
>> -jh-
>>
>> Bradley S. Huffman wrote:
>>
>>> Definitely a bug in SAXOutputter.addNsAttribute.
>>>
>>> Brad
>>>
>>> "Jan-Olof Hendig" writes:
>>>
>>>
>>>> This is a multi-part message in MIME format.
>>>>
>>>> --===============0573196067==
>>>> Content-Type: multipart/alternative;
>>>> boundary="----=_NextPart_000_0015_01C4BAD9.9ED698B0"
>>>>
>>>> This is a multi-part message in MIME format.
>>>>
>>>> ------=_NextPart_000_0015_01C4BAD9.9ED698B0
>>>> Content-Type: text/plain;
>>>> charset="iso-8859-1"
>>>> Content-Transfer-Encoding: quoted-printable
>>>>
>>>> Hello folks,
>>>>
>>>> Just to get it over with, I'm an XML newbie so keep the flame 
>>>> throwers =
>>>> away for a while please.
>>>>
>>>> Today I upgraded from JDOM beta 9 to the current release. As they 
>>>> say, =
>>>> better late than never :) My code needed very few changes in order to =
>>>> compile, the constructor for XMLOutputter had changed but that was 
>>>> easy =
>>>> to fix. However when running the code, see below, a small =
>>>> incompatibility surfaced.=20
>>>>
>>>> with beta 9 the code below produced the following
>>>>
>>>> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>>>> <message>
>>>>   <request>
>>>>      <onlyreturnoutdata>0</onlyreturnoutdata>
>>>>   </request>
>>>> </message>
>>>>
>>>> however with the current release I got
>>>>
>>>> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>>>> <message xmlns:=3D"">
>>>>   <request>
>>>>      <onlyreturnoutdata>0</onlyreturnoutdata>
>>>>   </request>
>>>> </message>
>>>>
>>>> as you can see the line containing the <message> tag suddenly 
>>>> includes =
>>>> the attribute 'xmlns:=3D""'
>>>>
>>>> My question is this, how do I get rid of this xmlns attribute so that =
>>>> the message looks exactly like it did in beta 9?
>>>>
>>>> Regards
>>>>
>>>> Jan-Olof Hendig
>>>>
>>>> /* --------------------- */
>>>>
>>>>
>>>> public class TestCase {
>>>> =20
>>>> protected static Document document;
>>>> protected static Element rootElement;
>>>> =20
>>>> public static void main(String[] args) {
>>>>  rootElement =3D new Element("message");
>>>>  Element requestElement =3D new Element("request");
>>>>  Element onlyReturnOutDataElement =3D new Element("onlyreturnoutdata");
>>>>  onlyReturnOutDataElement.addContent("0");
>>>> =20
>>>>  requestElement.addContent(onlyReturnOutDataElement);
>>>>  rootElement.addContent(requestElement);
>>>>  document =3D new Document(rootElement);
>>>>
>>>>  System.out.println(documentToString(document, true));
>>>> }
>>>> =20
>>>> =20
>>>> public static String documentToString(Document doc, boolean fIndent) {
>>>>  JDOMSource domSource =3D new JDOMSource(doc);
>>>>  StringWriter stringWriter =3D new StringWriter();
>>>>  StreamResult result =3D new StreamResult(stringWriter);
>>>>       =20
>>>>  TransformerFactory tFactory =3D TransformerFactory.newInstance();
>>>>  try {
>>>>   Transformer transformer =3D tFactory.newTransformer();
>>>>   if (fIndent) {
>>>>       transformer.setOutputProperty(OutputKeys.INDENT, "yes");
>>>>    =
>>>> transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount"= 
>>>>
>>>> , "3");
>>>>   }
>>>>   else
>>>>       transformer.setOutputProperty(OutputKeys.INDENT, "no");
>>>>   transformer.transform(domSource, result);         =20
>>>>  }=20
>>>>  catch (Exception e) {
>>>>   System.out.println(e); =20
>>>>  }
>>>>  return stringWriter.toString();
>>>> }
>>>> }
>>>>
>>>>
>>>>
>>>> ------=_NextPart_000_0015_01C4BAD9.9ED698B0
>>>> Content-Type: text/html;
>>>> charset="iso-8859-1"
>>>> Content-Transfer-Encoding: quoted-printable
>>>>
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
>>>> <HTML><HEAD>
>>>> <META http-equiv=3DContent-Type content=3D"text/html; =
>>>> charset=3Diso-8859-1">
>>>> <META content=3D"MSHTML 6.00.2900.2523" name=3DGENERATOR>
>>>> <STYLE></STYLE>
>>>> </HEAD>
>>>> <BODY bgColor=3D#ffffff>
>>>> <DIV><FONT face=3DArial size=3D2>Hello folks,</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>Just to get it over with, I'm an XML =
>>>> newbie so keep=20
>>>> the flame throwers away for a while please.</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>Today I upgraded from JDOM beta 9 to =
>>>> the current=20
>>>> release. As they say, better late than never :) My code needed very 
>>>> few =
>>>> changes=20
>>>> in order to compile, the constructor for XMLOutputter had changed but =
>>>> that was=20
>>>> easy to fix. However when running the code, see below, a small =
>>>> incompatibility=20
>>>> surfaced. </FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>with beta 9 the code below produced 
>>>> the =
>>>>
>>>> following</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>&lt;?xml version=3D"1.0"=20
>>>> encoding=3D"UTF-8"?&gt;<BR>&lt;message&gt;<BR>&nbsp;&nbsp;=20
>>>> &lt;request&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
>>>> &lt;onlyreturnoutdata&gt;0&lt;/onlyreturnoutdata&gt;<BR>&nbsp;&nbsp;=20
>>>> &lt;/request&gt;<BR>&lt;/message&gt;</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>however with the current release I =
>>>> got</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>&lt;?xml version=3D"1.0"=20
>>>> encoding=3D"UTF-8"?&gt;<BR>&lt;message 
>>>> xmlns:=3D""&gt;<BR>&nbsp;&nbsp;=20
>>>> &lt;request&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
>>>> &lt;onlyreturnoutdata&gt;0&lt;/onlyreturnoutdata&gt;<BR>&nbsp;&nbsp;=20
>>>> &lt;/request&gt;<BR>&lt;/message&gt;</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>as you can see the line containing 
>>>> the=20
>>>> &lt;message&gt; tag suddenly includes the attribute =
>>>> 'xmlns:=3D""'</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>My question is this, how do I get 
>>>> rid =
>>>> of this xmlns=20
>>>> attribute so that the message looks exactly like it did in beta =
>>>> 9?</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>Regards</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>Jan-Olof Hendig</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>/* --------------------- =
>>>> */</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2>public class TestCase=20
>>>> {<BR>&nbsp;<BR>&nbsp;protected static Document =
>>>> document;<BR>&nbsp;protected=20
>>>> static Element rootElement;<BR>&nbsp;<BR>&nbsp;public static void =
>>>> main(String[]=20
>>>> args) {<BR>&nbsp;&nbsp;rootElement =3D new=20
>>>> Element("message");<BR>&nbsp;&nbsp;Element requestElement =3D new=20
>>>> Element("request");<BR>&nbsp;&nbsp;Element onlyReturnOutDataElement 
>>>> =3D =
>>>> new=20
>>>> Element("onlyreturnoutdata");<BR>&nbsp;&nbsp;onlyReturnOutDataElement.add= 
>>>>
>>>> Content("0");<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;requestElement.addContent(on= 
>>>>
>>>> lyReturnOutDataElement);<BR>&nbsp;&nbsp;rootElement.addContent(requestEle= 
>>>>
>>>> ment);<BR>&nbsp;&nbsp;document=20
>>>> =3D new Document(rootElement);</FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial=20
>>>> size=3D2>&nbsp;&nbsp;System.out.println(documentToString(document,=20
>>>> true));<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;public static String=20
>>>> documentToString(Document doc, boolean fIndent) =
>>>> {<BR>&nbsp;&nbsp;JDOMSource=20
>>>> domSource =3D new JDOMSource(doc);<BR>&nbsp;&nbsp;StringWriter =
>>>> stringWriter =3D new=20
>>>> StringWriter();<BR>&nbsp;&nbsp;StreamResult result =3D new=20
>>>> StreamResult(stringWriter);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 
>>>>
>>>> =20
>>>> <BR>&nbsp;&nbsp;TransformerFactory tFactory =3D=20
>>>> TransformerFactory.newInstance();<BR>&nbsp;&nbsp;try=20
>>>> {<BR>&nbsp;&nbsp;&nbsp;Transformer transformer =3D=20
>>>> tFactory.newTransformer();<BR>&nbsp;&nbsp;&nbsp;if (fIndent)=20
>>>> {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
>>>> transformer.setOutputProperty(OutputKeys.INDENT,=20
>>>> "yes");<BR>&nbsp;&nbsp;&nbsp;&nbsp;transformer.setOutputProperty("{http:/= 
>>>>
>>>> /xml.apache.org/xslt}indent-amount",=20
>>>> "3");<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nb= 
>>>>
>>>> sp;&nbsp;&nbsp;&nbsp;=20
>>>> transformer.setOutputProperty(OutputKeys.INDENT,=20
>>>> "no");<BR>&nbsp;&nbsp;&nbsp;transformer.transform(domSource,=20
>>>> result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
>>>> &nbsp;&nbsp;<BR>&nbsp;&nbsp;}=20
>>>> <BR>&nbsp;&nbsp;catch (Exception e)=20
>>>> {<BR>&nbsp;&nbsp;&nbsp;System.out.println(e);&nbsp;&nbsp;<BR>&nbsp;&nbsp;= 
>>>>
>>>> }<BR>&nbsp;&nbsp;return=20
>>>> stringWriter.toString();<BR>&nbsp;}<BR>}<BR></FONT></DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
>>>> <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></BODY></HTML>
>>>>
>>>> ------=_NextPart_000_0015_01C4BAD9.9ED698B0--
>>>>
>>>>
>>>> --===============0573196067==
>>>> Content-Type: text/plain; charset="us-ascii"
>>>> MIME-Version: 1.0
>>>> Content-Transfer-Encoding: 7bit
>>>> Content-Disposition: inline
>>>>
>>>> _______________________________________________
>>>> To control your jdom-interest membership:
>>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>>> --===============0573196067==--
>>>
>>>
>>> _______________________________________________
>>> To control your jdom-interest membership:
>>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>>
> 


More information about the jdom-interest mailing list