-2

I have code which appends XML header to the existing xmlstring. In that I am getting response with XML declaration <?xml version="1.0" encoding="UTF-8"?> and I have add the attribute in my Tag like <Request xmlns="http://abc.com/ws/motor/v21">

Code

string  XMLString = '<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());

Output

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV: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" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xmlns="http://abc.com/ws/motor/v21">
<SOAP-ENV:Body>
<Request>
<AuthenticationDetail>
            <UserCredential>
              <Key>*******</Key>
              <Password>********</Password>
            </UserCredential>
          </AuthenticationDetail>
        </Request>
        </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

Expected Output

<SOAP-ENV: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" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<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>
1

1)I am getting response with XML declaration Why you want to remove the declaration XML Parser will parse it properly. 2) I have updated the code change that you are asking for below

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();
doc.load(XMLString);
DOM.Document docNew = new DOM.Document();
dom.XmlNode envelope= docNew.createRootElement('SOAP-ENV:Envelope',null,null);
envelope.setAttribute('xmlns:SOAP-ENV','http://schemas.xmlsoap.org/soap/envelope/');
envelope.setAttribute('xmlns:SOAP-ENC','http://schemas.xmlsoap.org/soap/encoding');
envelope.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
envelope.setAttribute('xmlns:xsd','http://www.w3.org/2001/XMLSchema');
envelope.setAttribute('xmlns:m0','http://abc.com/ws/motor/v21');
System.debug(envelope.getAttributeCount());
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()));
       }
   }else if (rootElementName=='Request'){
    domroot.setAttribute('xmlns','http://abc.com/ws/motor/v21');
   }
   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);
  }
 }
 string xmlValue=docNew.toXmlString().remove('<?xml version="1.0" encoding="UTF-8"?>');
  System.debug(xmlValue);
  • Thank you Vidhyasagaran . I want to remove XML declaration because I want to send this response to third party as http request. They not accepting the XML declaration. It's giving 400 bad request error – Mangoski Apr 25 at 3:11
  • You are giving them string as request? – Vidhyasagaran Muralidharan Apr 25 at 3:50
  • Yes I am passing the xml string as soap http request. Do you have any other suggestion to do it in a better way? – Mangoski Apr 25 at 8:46
  • Now check I have removed XML declaration – Vidhyasagaran Muralidharan Apr 25 at 9:20

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.