0

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

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/','');
root.setNamespace('a','http://example-ns1/');
root.setNamespace('b','http://example-ns2/');
System.debug(doc.toXmlString());

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.