2017-11-26 11 views
0

現在、SOAP Webサービスの呼び出しを開発しています。XML文書の要素を動的に追加する必要があります。私はこのためにdom4jライブラリを使用しようとしますが、これは他のケースではうまくいきますが、現在のXMLドキュメントでは実現できません。dom4jでxml文書の要素を追加する

私の目標は、<urn:sObjects>ノードの下に<fieldsToNull>fieldname</fieldsToNull>の要素を追加することです。それはこのように言った非常に簡単そうですが、私は悲しげにここ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> 

私はいくつかのトピックで見つかった答えに応じて私の最後の試みでの作業に問題があると思います。それはerrosを生成しませんが、動作しません:

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; 

は、誰もがそれを行う方法についての私のヒントを与えることができましたか?

精度:私のコードはTalendジョブで設定されます。それは働くべき方法を変えず、ただそれを言います。

答えて

1

より調査の後、私は最終的に私は次のコードを使用して欲しかった何をすることができるよ:

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; 
関連する問題