user2960190
user2960190

Reputation: 190

How to set multiple Namespace, one of them without prefix (JDom)

I need to set a namespace, I hope someone can help me.

That is what I need:

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I try to do this that way:

            Namespace ns1 = Namespace.getNamespace("urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
        Namespace ns2 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        Element root = new Element("Document");
        root.addNamespaceDeclaration(ns2);
        root.addNamespaceDeclaration(ns1);

But I get this exception:

The namespace xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" could not be added as a namespace to "Document": The namespace prefix "" collides with the element namespace prefix

Thanks

Upvotes: 2

Views: 2203

Answers (1)

wero
wero

Reputation: 32980

Use

Namespace ns1 = Namespace.getNamespace("urn:iso:std:iso:20022:tech:xsd:pain.001.001.03");
Namespace ns2 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Element root = new Element("Document", ns1);
root.addNamespaceDeclaration(ns2);

Since you have defined a default namespace you must use it when you create the Element. Else JDOM complains that the element is in no namespace and at the same time has a default namespace declaration.

Upvotes: 3

Related Questions