使用 javax.xml.soap 创建 SOAP

Creating a SOAP using javax.xml.soap

我一直在尝试使用 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>

尝试将 SOAP 转换为字符串,然后使用 replaceAll of String 替换您想要获取的文本,然后将其转换回 SOAP

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;