1

我面临这个问题一个多月了,所以我会对你的帮助感到非常高兴,事实上我正在询问一种可以让我解析 SOAP 消息(请求)以检索所需信息的方法,例如安全信息(如果有的话)和来自消息正文的信息


感谢您回答我,但知道我正在处理另一个问题,即 WS-SecurityPolicy,我必须最终解析一个像这样的 xml 文件: <?xml version="1.0" encoding="UTF-8"?>

    <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <wsp:Policy>
        <sp:TransportToken>
          <wsp:Policy>
            <sp:HttpsToken RequireClientCertificate="false"/>
          </wsp:Policy>
        </sp:TransportToken>
        <sp:AlgorithmSuite>
          <wsp:Policy>
            <sp:Basic128/>
          </wsp:Policy>
        </sp:AlgorithmSuite>
        <sp:Layout>
          <wsp:Policy>
            <sp:Lax/>
          </wsp:Policy>
        </sp:Layout>
        <sp:IncludeTimestamp/>
      </wsp:Policy>
    </sp:TransportBinding>
    <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
        <wsp:Policy>
            <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient" />
      </wsp:Policy>
    </sp:SignedSupportingTokens>

'

知道这个 XML 文件名为 Policy.xml 并且包含必须存在的 WS-SecurityPolicy 规则。

4

3 回答 3

7

这取决于您的消息中包含的内容。您已经标记了您的问题,jaxb这让我认为您在肥皂消息中有 xml 序列化数据。如果是这种情况,您可以使用 JAXB unmarshaller 将您的消息转换为 Java 类的实例:

JAXBContext jbc = JAXBContext.newInstance("com.mypackage");
Unmarshaller um = jbc.createUnmarshaller();

JAXBElement<MyClass> element = um.unmarshal(parameterNode, MyClass.class);
MyClass data = element.getValue();
于 2011-09-13T13:55:27.027 回答
3

我发现这段代码可以从 SOAPResponse 消息解组到 POJO 类,希望对您有所帮助。

    //unmarshalling
    JAXBContext jc = JAXBContext.newInstance(MyPOJO.class);
    Unmarshaller um = jc.createUnmarshaller();
    MyPOJO output = (MyPOJO)um.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument());
于 2014-06-27T23:38:12.403 回答
1

我猜你已经有一个肥皂对象并且想要解析消息的内容。

// assumptions: soapMessage contains the SoapMessage
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        soapMessage.writeTo(baos);
        final InputSource inputSource = new InputSource(new StringReader(
            new String(baos.toByteArray())));
        final DocumentBuilderFactory dbFactory = DocumentBuilderFactory
            .newInstance();
        final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        final Document doc = dBuilder.parse(inputSource);
        doc.normalize();
// after this use Xpath to process the soapMessage

如果您将肥皂消息作为字符串,那么您可以从使用字符串构建 Document 对象开始。

于 2011-09-13T13:29:46.457 回答