1

Step - 1: Create xml structure

Xmlstreamwriter xmlW = new Xmlstreamwriter();
xmlW.writeStartDocument('utf-8','1.0');
//Salesinvoices
xmlW.writeStartElement(null,'SalesInvoices', null);
xmlw.writeDefaultNamespace('urn:xmlns:http://www.soapservice.com:salesinvoices');
xmlw.writeAttribute('xmlns','','xsi','http://www.w3.org/2001/XMLSchema-instance');
//SalesInvoice
xmlW.writeStartElement(null,'SalesInvoice',null);
//Reference
xmlW.writeStartElement(null,'Reference',null);
xmlW.writeCharacters('xx-112345');
xmlW.writeEndElement();//Reference
//Reference1
xmlW.writeStartElement(null,'Reference1',null);
xmlW.writeCharacters('xx-112345');
xmlW.writeEndElement();//Reference1
xmlW.writeEndElement();//SalesInvoice
xmlW.writeEndElement();//Salesinvoices
xmlW.writeEndDocument();
Dom.document doc1 = new dom.document();
doc1.load(xmlw.getXmlString());
dom.xmlnode xml1 = doc1.getrootelement();
system.debug(doc1.toxmlstring());

Step -2 : pass this xml to a method which creates parent xml nodes with sessionid and header before passing to web service

String str = '<sessionId></sessionId>';
Dom.document doc = new dom.document();
doc.load(str);
dom.xmlnode xml = doc.getrootelement();

for(DOM.XmlNode anyNode: xml1.getChildElements()){
    Dom.XmlNode anyElementNode = xml.addChildElement(anyNode.getName(), anyNode.getNamespace(), '');
}
system.debug(doc.toxmlstring());

Issue: inside the for loop to get child elements, it is detecting only main child of the incoming xml which is "SalesInvoice" and final output is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<sessionId>
    <SalesInvoice xmlns="urn:xmlns:http://www.soapservice.com:salesinvoices" />
</sessionId>

while it is expected to be as follows:

<?xml version="1.0" encoding="UTF-8"?>
<sessionId>
    <SalesInvoices xmlns="urn:xmlns:http://www.soapservice.com:salesinvoices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <SalesInvoice>
            <Reference>xx-112345</Reference>
            <Reference1>xx-112345</Reference1>
        </SalesInvoice>
    </SalesInvoices>
</sessionId>

I tried the following but still output is same:

String str = '<sessionId></sessionId>';
Dom.document doc = new dom.document();
doc.load(str);
dom.xmlnode xml = doc.getrootelement();
Dom.XmlNode anyElementNode = listN(xml, xml1);

public Dom.xmlnode listN(DOM.XmlNode resultnode, dom.xmlnode parentNode){
    resultnode.addChildElement(parentNode.getName(), parentNode.getNamespace(), '');
    if(parentNode.getchildelements() != null && parentNode.getchildelements().size() > 0) {
        for(Dom.xmlnode y : parentNode.getchildelements()) {
            if(y.getchildelements() != null && y.getchildelements().size() > 0) {
                listN(parentNode, y);
            }             
        }
    }
    return resultnode;
}
4
  • Are you able to share the WSDL for this web service? It might be easier to just generate the required Apex code directly from the WSDL. Mar 12, 2017 at 19:53
  • Ballinger, Thanks for your reply and this is continution to the earlier post on which you replied. http://salesforce.stackexchange.com/questions/162132 WSDL url is http://api.yukiworks.nl/ws/Sales.asmx?wsdl
    – sf_user
    Mar 12, 2017 at 19:55
  • Was the XML generated by the FuseIT SFDC Explorer version of WSDL2Apex not working as a basis for this? Mar 12, 2017 at 20:00
  • I tried as is and found that there is issue while parsing child elements... so I am trying to make some modifications in the place where it is parsing
    – sf_user
    Mar 12, 2017 at 20:02

1 Answer 1

2

You will need to keep walking down the children of the Dom.XmlNode records to copy out the inner nodes.

// ... Your first block of code that builds up xm1 from Step 1

String str = '<sessionId></sessionId>';
Dom.document doc = new dom.document();
doc.load(str);
dom.xmlnode xml = doc.getrootelement();

for(DOM.XmlNode anyNode : xml1.getChildElements()){
    Dom.XmlNode anyElementNode = xml.addChildElement(anyNode.getName(), anyNode.getNamespace(), '');
    for (Dom.XmlNode child : anyNode.getChildren()) {
        Dom.XmlNode innerChildNode = anyElementNode.addChildElement(child.getName(), child.getNamespace(), '');
        for (Dom.XmlNode child1 : child.getChildren()) {
            // Assuming the XMLNodeType is TEXT here.
            // Should really recursively check the NodeType at each level
            string body = child1.getText();
            Dom.XmlNode childTextNode = innerChildNode.addTextNode(body);
        }

    }


}
system.debug(doc.toxmlstring());

In practice you wouldn't clone XML nodes like this. Instead use a recursive method that checks the Dom.XmlNodeType using getNodeType().

0

You must log in to answer this question.

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