将SOAP消息解组到java /或简单地解析SOAP消息?

时间:2011-09-13 13:05:41

标签: java soap jaxb unmarshalling saaj

我面临这个问题超过一个月,所以我真的很高兴你的帮助,实际上我问的是一种可以让我解析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规则。

3 个答案:

答案 0 :(得分:5)

这取决于您的消息内容。您已使用jaxb标记了您的问题,这让我觉得您在soap消息中有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();

答案 1 :(得分:2)

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

    //unmarshalling
    JAXBContext jc = JAXBContext.newInstance(MyPOJO.class);
    Unmarshaller um = jc.createUnmarshaller();
    MyPOJO output = (MyPOJO)um.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument());

答案 2 :(得分:1)

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

// 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

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