Java se soap定制

时间:2015-04-17 06:57:20

标签: java soap

我试图创建一个肥皂请求,我从中学习 https://stackoverflow.com/a/15949858/4799735 从那个教程,我感到困惑,如何创建像这样的XML

<GetUserInfo>
  <ArgComKey Xsi:type="xsd:integer"> ComKey </ ArgComKey> 
 <Arg> 
  <PIN Xsi:type="xsd:integer"> Job Number </ PIN> 
 </ Arg> 
</ GetUserInfo>

我的工作代码是

SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("GetUserInfo");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("type", "ArgComKey","xsd");
    SOAPBodyElement element = soapBody.addBodyElement(envelope.createName("type", "ArgComKey", "=xsd:integer"));
    soapBodyElem1.addTextNode("ComKey");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("Arg");
    soapBodyElem2.addTextNode("123");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI + "VerifyEmail");

它的回归

<GetUserInfo>
<ArgComKey:type xmlns:ArgComKey="xsd">ComKey</ArgComKey:type>
<Arg>123</Arg>
</GetUserInfo><ArgComKey:type xmlns:ArgComKey="=xsd:integer"/>

我的问题是我必须编写的内容,以便我的代码结果为

<ArgComKey Xsi:type="xsd:integer"> ComKey </ ArgComKey>

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement( "GetUserInfo" );
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement( "ArgComKey", "", "xsd:integer" );
soapBodyElem1.addTextNode( "ComKey" );
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement( "Arg" );
soapBodyElem2.addTextNode( "123" );

这将导致:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <GetUserInfo>
            <ArgComKey xmlns="xsd:integer">ComKey</ArgComKey>
            <Arg>123</Arg>
        </GetUserInfo>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如果您想使用xsi命名空间,请尝试使用以下代码:

SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement( "GetUserInfo" );
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement( "ArgComKey" );
soapBodyElem1.addTextNode( "ComKey" ).setAttribute("xsi:type","xsd:integer");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement( "Arg" );
soapBodyElem2.addTextNode( "123" );

多亏了这一点,您将收到:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <GetUserInfo>
            <ArgComKey xsi:type="xsd:integer">ComKey</ArgComKey>
            <Arg>123</Arg>
        </GetUserInfo>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>