Reputation: 11
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.
Upvotes: 0
Views: 1926
Reputation: 11
After more investigation I'm finally able to do what I wanted with the following code :
org.dom4j.Document msgDocument = ((routines.system.Document)input_row.Body).getDocument();
Map uris = new HashMap();
uris.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
uris.put("urn", "urn:partner.soap.sforce.com");
XPath xpath = msgDocument.createXPath("//soapenv:Body/urn:update/urn:sObjects");
xpath.setNamespaceURIs(uris);
List<Node> nodes = xpath.selectNodes(msgDocument);
for (Node node : nodes) {
Element e = (Element) node;
Element fieldsToNull = e.addElement("fieldsToNull");
fieldsToNull.setText("Name");
System.out.println(fieldsToNull.toString());
}
routines.system.Document newMsgDocument = new routines.system.Document();
newMsgDocument.setDocument(msgDocument);
output_row.Body = newMsgDocument;
Upvotes: 1