[jdom-interest] Small but irritating problem

Bradley S. Huffman hip at cs.okstate.edu
Tue Oct 26 10:03:17 PDT 2004


"Jan-Olof Hendig" writes:

> 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?

Yes, that workaround won't work since the problem is in SAXOutputter and
not your document.  First there is a serious problem in addNsAttribute
which is causing a well-formness error, xmlns:="" is not well-formed.

Second is SAXOutputter is always reporting the default namespace, which may or
may not be a problem.

Below is a patch to correct the well-formness error. The second problem is
going to take a little more exploration.

Brad

*** SAXOutputter.java	Mon Oct 25 21:59:16 2004
--- SAXOutputter.new	Mon Oct 25 22:00:38 2004
***************
*** 1215,1225 ****
              if (atts == null) {
                  atts = new AttributesImpl();
              }
!             atts.addAttribute("",                          // namespace
!                               "",                          // local name
!                               "xmlns:" + ns.getPrefix(),   // qualified name
!                               "CDATA",                     // type
!                               ns.getURI());                // value
          }
          return atts;
      }
--- 1215,1236 ----
              if (atts == null) {
                  atts = new AttributesImpl();
              }
! 
!             String prefix = ns.getPrefix();
!             if (prefix.equals("")) {
!                 atts.addAttribute("",                        // namespace
!                                   "",                        // local name
!                                   "xmlns",                   // qualified name
!                                   "CDATA",                   // type
!                                   ns.getURI());              // value
!             }
!             else {
!                 atts.addAttribute("",                        // namespace
!                                   "",                        // local name
!                                   "xmlns:" + ns.getPrefix(), // qualified name
!                                   "CDATA",                   // type
!                                   ns.getURI());              // value
!             }
          }
          return atts;
      }


More information about the jdom-interest mailing list