Force the default namespace of an XML fragment

Pascal Maugeri <[email protected]>
Newsgroups gmane.text.xml.xmlbeans.user
Message-ID <[email protected]>
Hi

I'm writing a method to set the default namespace of a given XML fragment:

    public static XmlObject setDefaultNamespace(XmlObject xmlFragment,
String namespace) {

        XmlCursor eltCursor = xmlFragment.newCursor();
        if (eltCursor.currentTokenType().isStartdoc())
            eltCursor.toNextToken();

        String defaultNs = eltCursor.namespaceForPrefix("");
        if (defaultNs == null || defaultNs.length() == 0) {
            System.out.println("No default namespace detected!");
            eltCursor.insertNamespace("", namespace);
        }

        eltCursor.dispose();
        return xmlFragment;
    }

The call to "eltCursor.insertNamespace("", namespace);" does not work, it
doesn't set the default namespace in the result returned. I observed that it
works only when the prefix is set (eg. eltCursor.insertNamespace("pre",
namespace);).

What am I doing wrong ?

Thanks in advance for any help
Pascal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.