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;
}
http://salesforce.stackexchange.com/questions/162132
WSDL url ishttp://api.yukiworks.nl/ws/Sales.asmx?wsdl