0

How to do i construct the xml request formation in apex,required output format is pasted here.

<ns:Email EmailType="03"><ns:EmailAddress>[email protected]</ns:EmailAddress>
 </ns:Email> 

I have tried the following code

DOM.Document doc = new DOM.Document();  
String soapNS = 'http://www.w3.org/2003/05/soap-envelope';
String xsi = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
String serviceNS = '';
String serviceStart = '';       
dom.XmlNode envelope = doc.createRootElement('Envelope',soapNS, 'soap');
envelope.setNamespace('ns',serviceNS);
dom.XmlNode bodyhead = envelope.addChildElement('soap:Header',null,null);
dom.XmlNode security = bodyhead.addChildElement('Security',xsi,'');
dom.XmlNode ContactBdy = bodyhead.addChildElement('ns:Contact', null, null);
dom.XmlNode EmailsBdy = ContactBdy.addChildElement('ns:Email', 'EmailType="03"',  null);
EmailsBdy.addChildElement('ns:EmailAddress',null,null).addTextNode('[email protected]');
System.debug('::::doc::::::'+doc.toXmlString());

2 Answers 2

0

EmailType is not a namespace, it's an attribute. The following change to your code should work.

dom.XmlNode EmailsBdy = ContactBdy.addChildElement('Email', null, 'ns');
EmailsBdy.setAttribute('EmailType','03');
1
  • Upvoted answer.Thank you
    – Jai_Minds
    Jun 19, 2019 at 18:38
0

The arguments to addChildElement (see the XmlNode documentation) are:

  • name
  • namespace
  • prefix

so each call to that should be supplying all 3 values given that the XML you need to generate has several namespaces:

private static final String PAYLOAD_NS = 'http://...';
private static final String PAYLOAD_PREFIX = 'ns';

EmailsBdy.addChildElement('EmailAddress', PAYLOAD_NS, PAYLOAD_PREFIX)

You must log in to answer this question.

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