繁体   English   中英

Java SE Soap定制

[英]Java se soap customize

我正在尝试创建一个肥皂请求,我从那是教程的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>

您可以尝试以下操作:

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM