[jdom-interest] Special XMLOutputter which omits namespaces?

Per Norrman pernorrman at telia.com
Tue May 18 14:33:30 PDT 2004


Hi,

why don't you just set the namespace for all
elements to Namespace.NO_NAMESPACE before dumping.
If you need to retain the original tree, you can
always clone the subtree.

Here's a snippet that removes all namespaces and
namespace declarations:

void removeNamespaces(Element element) {
    element.setNamespace(null);
    List tmp = new ArrayList(element.getAdditionalNamespaces());
    for (Iterator i = tmp.iterator(); i.hasNext();) {
       element.removeNamespaceDeclaration((Namespace)i.next());
    }
    for (Iterator i = element.getChildren().iterator(); i.hasNext();) {
       removeNamespaces((Element)i.next());
    }
}


/pmn

Guillaume Berche wrote:
> Hello,
> 
> I've been pretty happy with JDom b8 except for the special need that I have
> to output some XML fragment without including the namespace prefixes in each
> element. The use-case for this is that I obtain a JDom tree from an XML
> document using Xerces with schema validation on. I obtain a valid JDom tree
> with the proper namespaces. I then want to dump a part of that tree without
> specifying the namespace of each element.
> 
> I had looked at various threads on this list a while ago and concluded the
> best way for me to do this was to create a new "NoNameSpaceXMLOutputter"
> based on the org.jdom.output.XMLOutputter and just modify the behavior of
> the private     "void printNamespace(Namespace ns, Writer out,
> NamespaceStack namespaces)" method, to have it return immediately, and not
> insert namespaces element prefixes.
> 
> I wonder whether as part of the beta 10, there is better way of using JDom
> than duplicating the XMLOutputter code. I briefly looked at the new
> org.jdom.output.Format class but could not find something corresponding to
> my need.
> 
> I realize this may be a specific need and may not justify extension in the
> Format mechanism for the 1.0, but it may be interesting to support wider
> subclassing of the org.jdom.output.XMLOutputter class by having some of its
> methods now be exposed as "protected" instead of "private". So for the final
> 1.0, I'd then advocate for the "void printNamespace(Namespace ns, Writer
> out, NamespaceStack namespaces)" method, so my NoNameSpaceXMLOutputter class
> would simply override this method instead of having to duplicate the whole
> class.
> 
> Thanks for your help and for this great library!
> 
> Regards,
> 
> Guillaume.
> 
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
> 




More information about the jdom-interest mailing list