[jdom-interest] DOMOutputter.output(Element ) adds extra namespace

gally gahlawat gally_luv at hotmail.com
Mon Jun 3 05:43:47 PDT 2002


************************
on beta 7.0, DOMBuuilder.build(Element) converts a JDOM element into w3c
element with an additional namespace if there is already a namespace in the
jdom element. following is the code and results:
************************


**************************************
method to create an element:
------------------------------
private org.w3c.dom.Element createHeader(String uid,String pswd) throws
JDOMException, SOAPException
{
       org.w3c.dom.Element headerElement = null;
       try
       {
         Namespace rootNameSpace =
Namespace.getNamespace("gally","urn:gally-soap-message");
         Element rootElement = new Element("authenticate",rootNameSpace);


         Element uidElement = new Element("uid",rootNameSpace);
         uidElement.removeNamespaceDeclaration(rootNameSpace);
         Element pswdElement = new Element("pswd",rootNameSpace);
         pswdElement.removeNamespaceDeclaration(rootNameSpace);

         pswdElement.addContent(pswd);
         uidElement.addContent(uid);

         rootElement.addContent(uidElement);
         rootElement.addContent(pswdElement);

         DOMOutputter outputter = new DOMOutputter();
         headerElement = outputter.output(rootElement);

         Document doc = new Document(rootElement);
         // test the document
         System.out.println("header at client: ");
         XMLOutputter xmlOutPutter = new XMLOutputter("    ",true);
         xmlOutPutter.setTextNormalize(true);
         xmlOutPutter.setOmitDeclaration(true);
         xmlOutPutter.output(doc,System.out);
       }
       catch(IOException e)
       {
         e.printStackTrace();
       }
       return headerElement;
}
******************************************************************
prints the following: depending on the value of uid and pswd
<gally:authenticate xmlns:gally="urn:gally-soap-message">

   <gally:uid>IT656</gally:uid>

   <gally:pswd>********</gally:pswd>

</gally:authenticate>
*****************************************************************

*****************************************************
code to print w3c element:
notice that the printout above is before DOMBuilder is used to convert:

---------------------
org.w3c.dom.Element headerElement =
soapAuthenticateClient.createHeader("IT656   ","********");
     StringWriter strWriter = new StringWriter();


DOM2Writer.serializeAsXML(((org.w3c.dom.Node)headerElement),strWriter);


     System.out.println(" element header after conversion: " +
strWriter.toString()); // DOM2Writer is apache soap tool
************************************************
prints the following:
-------------------------------------
element header after conversion: <gally:authenticate
xmlns:gally="urn:gally-soap-message"
xmlns:gally="urn:gally-soap-message"><gally:uid>IT656
</gally:uid><gally:pswd>inn0vate</gally:pswd></gally:authenticate>
***********************************************************************

*************************************
notice the 2 xmlns:gally declarations
now the workaround: remove the attribute from w3c element:
org.w3c.dom.Element headerElement =
soapAuthenticateClient.createHeader("IT656   ","********");
     StringWriter strWriter = new StringWriter();
-----------------------------------------------------------
     headerElement.removeAttribute("xmlns:gally");
-------------------------------------------------------------


DOM2Writer.serializeAsXML(((org.w3c.dom.Node)headerElement),strWriter);//
DOM2Writer is org.apache.soap.util.xml.DOM2Writer


     System.out.println(" element header after conversion: " +
strWriter.toString());

**************************************************************

prints out the correct:
element header after conversion: <gally:authenticate
xmlns:gally="urn:gally-soap-message"><gally:uid>IT656
</gally:uid><gally:pswd>inn0vate</gally:pswd></gally:authenticate>
***************************************

notice the extra attribute is gone !!
can somebody please tell me if this is fixed in beta 8.0 or not..
thanks and regard's
karun


_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx




More information about the jdom-interest mailing list