我一直在尝试使用javax.xml.soap编写SOAP Web服务,但是我获得的输出并不是我期望的。
我的预期输出为:
<test:testment
xmlns:test="http://www.shopper.com/schemas/CMS_Generic/testment_Request.xsd"
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<test:RequestHeader>
<test:MessageId>171004081257000_3106</test:MessageId>
<test:MsgSource>MUTUALIND</test:MsgSource>
<test:ClientCode>MUTUALIND</test:ClientCode>
<test:BatchRefNmbr>171004081257000_3106</test:BatchRefNmbr>
</test:RequestHeader>
<test:InstrumentList>
<test:instrument>
<test:InstRefNo>171004081257000_3106</test:InstRefNo>
<test:MyProdCode>NETtest</test:MyProdCode>
<test:TxnAmnt>99.5</test:TxnAmnt>
<test:AccountNo>650000173</test:AccountNo>
<test:testmentDt>2017-10-04</test:testmentDt>
<test:RecBrCd>BOFA0BG3978</test:RecBrCd>
<test:BeneAcctNo>1234569874</test:BeneAcctNo>
<test:BeneName>INDIA TEST TEST</test:BeneName>
<test:BeneAddr1>IND</test:BeneAddr1>
<test:city>IND</test:city>
<test:InstDt>2017-10-04</test:InstDt>
<test:testmentDtl1>Mumbai</test:testmentDtl1>
<test:testmentDtl2>UNITED KINGDOM</test:testmentDtl2>
<test:EnrichmentSet>
<test:Enrichment>TEST
CLIENT~SAVING~TEST~09582650000173~FAMILY_MAINTENANCE~1234569874
</test:Enrichment>
</test:EnrichmentSet>
</test:instrument>
</test:InstrumentList>
</test:testment>
尝试将肥皂转换为字符串,而不是将字符串的替换与您要获得的文本替换,然后将其转换回肥皂
ByteArrayOutputStream out = new ByteArrayOutputStream();
soapMessage.writeTo(out);
//converting to String so that replacing certain soap tags could be easy
String strMsg = new String(out.toByteArray());
strMsg = strMsg.replaceAll("SOAP-ENV", "test");
strMsg = strMsg.replaceAll("Header", "RequestHeader");
strMsg = strMsg.replaceAll("Envelope", "testment");
strMsg = strMsg.replaceAll("Body", "InstrumentList");
strMsg = strMsg.replaceAll("xmlns:pay="http://www.w3.org/2003/05/soap-envelope"",
"xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"");
//converting String back to SOAP
InputStream is = new ByteArrayInputStream(strMsg.getBytes());
soapMessage = MessageFactory.newInstance().createMessage(null, is);
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;