-1

I have XML string like below in that I have to add root element and some attribute using apex. I was searching this for whole day but I din't get any key point. I saw there is some support for DOM and XML stream reader/writer but I am not sure how to achieve this.

My XML String

<Request>
  <AuthenticationDetail>
    <UserCredential>
      <Key>*******</Key>
      <Password>********</Password>
    </UserCredential>
  </AuthenticationDetail>
</Request>

Expected XML String

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://abc.com/ws/motor/v21">
<SOAP-ENV:Body>
<Request xmlns="http://abc.com/ws/motor/v21">
      <AuthenticationDetail>
        <UserCredential>
          <Key>*******</Key>
          <Password>********</Password>
        </UserCredential>
      </AuthenticationDetail>
    </Request>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

If any one achieved this in Apex can you please share the code or any pointer/information is highly appreciated. I am struggling in this for past 2 days.

1
      string  XMLString = '<?xml version="1.0" encoding="UTF-8"?> <Request><AuthenticationDetail><UserCredential><Key>*******</Key><Password>********</Password></UserCredential></AuthenticationDetail></Request>';
      DOM.Document doc=new DOM.Document();
      String soapenv = 'http://schemas.xmlsoap.org/soap/envelope/';
      String soapenc = 'http://schemas.xmlsoap.org/soap/encoding';
      String xsi='http://www.w3.org/2001/XMLSchema-instance';
      String xsd='http://www.w3.org/2001/XMLSchema'; 
      String m0='http://abc.com/ws/motor/v21';
      doc.load(XMLString);
     DOM.Document docNew = new DOM.Document();
    dom.XmlNode envelope= docNew.createRootElement('Envelope',SOAPENV,'SOAP-ENV');
    envelope.setNamespace('SOAP-ENV',SOAPENV);
    envelope.setNamespace('xmlns',m0);
    envelope.setAttributeNS('SOAP-ENV',soapenv,m0,null);
    envelope.setAttributeNS('SOAP-ENC',soapenc,m0,null);
    envelope.setAttributeNS('xsi',xsi,m0,null);
    envelope.setAttributeNS('xsd',xsd,m0,null);
    envelope.setAttributeNS('m0',m0,m0,null);
    envelope.removeAttribute('SOAP-ENV',m0);
    envelope.removeAttribute('xmlns',m0);
    dom.XmlNode body = envelope.addChildElement('SOAP-ENV:Body',null,null);
    //adding the root Element into the new document
    dom.XmlNode rootNode = doc.getRootElement();
      string rootElementName=rootNode.getName();
      string rootElementText=rootNode.getText();
      string rootElementNamespace=rootNode.getNamespace();
      string rootElementprefix=null;
      if(rootElementNamespace!=null)
      {
        rootElementprefix= rootNode.getPrefixFor(rootElementNamespace);
      }
    dom.XmlNode domroot = body.addChildElement(rootElementName,rootElementNamespace,rootElementprefix);
    if(rootNode.getAttributeCount()>0){
        for(integer i=0;i<rootNode.getAttributeCount();i++){
           domroot.setAttribute(rootNode.getAttributeKeyAt(i),rootNode.getAttributeValue(rootNode.getAttributeKeyAt(i),rootNode.getNamespace()));
           }
    }
    if(rootNode.getText()!=null){
             domroot.addTextNode(rootNode.getText());
        }

   process(doc.getRootElement().getChildElements(),domroot);

   //Iterating every child element of the doc and adding it in doc new
   Public void process(Dom.XmlNode[] domlist,dom.XmlNode domroot){
    for (Dom.XmlNode child : domlist) {
        Dom.XmlNode addition = domroot.addChildElement(child.getName(),child.getNamespace(),child.getNamespace()!=null ? child.getPrefixFor(child.getNamespace()) :null);
        if(child.getAttributeCount()>0){
        for(integer i=0;i<child.getAttributeCount();i++){
           addition.setAttribute(child.getAttributeKeyAt(i),child.getAttributeValue(child.getAttributeKeyAt(i),child.getNamespace()));
           }
        }
        if(child.getText()!=null){
             addition.addTextNode(child.getText());
        }
        System.debug(child.getChildElements());
        process(child.getChildElements(),addition);
      }
     }
    System.debug(docNew.toXmlString());

I have updated the code.Now You can add envelope and body can be added with the response.

7
  • @vidyasagaran. Thank you so much for your code but my scenario is I am getting the XML string mentioned above as response from third party . In that XML string I need to append the envelope and body tag. I can't create the whole XML again. – Mangoski Apr 21 '19 at 18:05
  • Now I have modified the code as per your comment – Vidhyasagaran Muralidharan Apr 22 '19 at 0:46
  • Thank you so much. This code really helps me out to learn a lot about XML serialization. I have some clarification. If i don't want to have the below attribute in the envelope tag xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xmlns ="http://abc.com/ws/motor/v21" and I also don't want to create the root tag <?xml version="1.0" encoding="UTF-8"?> How I can achieve this. – Mangoski Apr 24 '19 at 13:30
  • Its not added the attribute also in the <request> tag <Request xmlns="http://abc.com/ws/motor/v21">. I got stuck in the above two points. one is removing the XML Declaration and xmlns:xmlns attribute and second one is adding the namespace in the <request> tag. – Mangoski Apr 24 '19 at 19:20
  • updated all the changes in this post:salesforce.stackexchange.com/questions/259979/… – Vidhyasagaran Muralidharan Apr 25 '19 at 3:08

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.