I currently developing a call to a SOAP Web services, an I need to be able to add elements in my XML document dynamically. I try to use for this the dom4j library, which works well in others cases, but I can't arrive to do it with my current XML document.
My goal is to add some <fieldsToNull>fieldname</fieldsToNull> elements under the <urn:sObjects> node. It seems quite simple said like this but I think I sadly have an issue in working with XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com" xmlns:urn1="urn:sobject.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <urn:SessionHeader> <urn:sessionId>sessionId</urn:sessionId> </urn:SessionHeader><urn:AssignmentRuleHeader> <urn:assignmentRuleId/><urn:useDefaultRule>1</urn:useDefaultRule></urn:AssignmentRuleHeader> </soapenv:Header> <soapenv:Body> <urn:update> <urn:sObjects> <type>Account</type> <Id/> <Name/> </urn:sObjects> </urn:update> </soapenv:Body> Here my last attempt according to answers I found on some topics. It doesn't generate erros but don't work :
org.dom4j.Document msgDocument = ((routines.system.Document)input_row.Body).getDocument(); org.dom4j.Element root = msgDocument.getRootElement(); String xpathExpression = "Enveloppe.Body.update.Objects"; List<Node> nodes = root.selectNodes(xpathExpression); for (Node node : nodes) { Element e = (Element) node; e.addElement("fieldsToNull").addText("Name"); } routines.system.Document newMsgDocument = new routines.system.Document(); newMsgDocument.setDocument(msgDocument); output_row.Body = newMsgDocument; Did anyone can give my a hint on how to do it ?
Precision : my code will be set in a Talend job. It doesn't change the way it should work but just to say it.