How can I add two or more namespace on root element in a XML Document?

In Java, I create the XMLNode with two namespace and then I set it as root element in a XML Document. In Salesforce, I have just read the documentation and it isn't available a constructor for the XMLNode. So, I must create directly a root element from a Dom.Document object and I can set only a namespace.

I don't find a solution. Thanks a lot!

1 Answer 1


You can set as many namespaces as you'd like with setNamespace. Here's a trivial execute anonymous script:

Dom.Document doc = new Dom.Document();
Dom.XmlNode root = doc.createRootElement('root','http://defaultns/','');

Output (note: This is cleaned up for legibility):

<?xml version="1.0" encoding="UTF-8"?>
  <root xmlns="http://defaultns/" 
        xmlns:b="http://example-ns2/" />

Observe how root now has three namespaces, the default namespace, plus "a" and "b" namespaces.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .