我有一个soap + xml文件,该文件用于发送HttpUrlConnection正文。在发送POST之前,我需要编辑一些值,如下所示:
sopaXML.xml:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:int="http://service.com/integration/">
<soap:Header>
<int:Options>
<int:UpdateLastModified>true</int:UpdateLastModified>
</int:Options>
</soap:Header>
<soap:Body>
<int:Execute>
<int:commandRequest xsi:type="int:TaskCreateCommand" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<int:TaskOrder>
<int:TypeCategory>Request</int:TypeCategory>
<int:Customer>
<int:Id>83</int:Id>
</int:Customer>
<int:Items>
<int:WoItem>
<int:Task>
<int:Id>16519</int:Id>
</int:Task>
<int:Comment>New Task 1</int:Comment>
</int:WoItem>
</int:Items>
</int:TaskOrder>
</int:commandRequest>
</int:Execute>
</soap:Body>
</soap:Envelope>
需要更改值:
<int:TaskOrder>
<int:TypeCategory>Request</int:TypeCategory>
<int:Customer>
<int:Id>102</int:Id>
</int:Customer>
<int:Items>
<int:WoItem>
<int:Task>
<int:Id>20034</int:Id>
</int:Task>
<int:Comment>Modified New Task Here!</int:Comment>
</int:WoItem>
</int:Items>
</int:TaskOrder>
我尝试过的代码:
String url = "http://url/wsdk/Service.asmx";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/soap+xml");
con.setRequestProperty("Cookie", "SessionId=0t10; path=/; HttpOnly");
con.setDoOutput(true)
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
XML xml = new XMLDocument(new File("src/main/resources/soapXML.xml"));
String xmlString = xml.toString();
System.out.println(xmlString);
InputStream stream = new ByteArrayInputStream(xmlString.getBytes());
SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, stream);
// How to modify the values
String modifiedxmlString = // modidfied soapXML string
wr.writeBytes(modifiedxmlString);
wr.flush();
wr.close();
String responseStatus = con.getResponseMessage();
如何更改soap + xml字符串中的现有值并将修改后的字符串发布到HttpURLconnection中?
答案 0 :(得分:0)
尝试下面给出的代码。
注意:其提示不是经过编译的代码
您有SOAPMessage
个对象,可以用来获取肥皂体
SOAPMessage sm = -----
SOAPBody body = sm.getSOAPBody();
private void updateNodeValue(List<String> nodeNamesWhichYouWantToUpdate, SOAPBody body) {
for(String nodeName : nodeNamesWhichYouWantToUpdate){
NodeList nodeList = body.getElementsByTagName(nodeName);
int length = nodeList.getLength();
for (int i = 0; i < length; i++) {
Node node = (Node) nodeList.item(i);
node.setTextContent("updateNodeValue");
}
}
}