在 Java 中从 SOAP 消息中获取字符串 [英] Get Strings from SOAP Message in Java

查看:42
本文介绍了在 Java 中从 SOAP 消息中获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 SOAP 消息中获取特定部分并获取它们的值?

How can I get specific parts from a SOAP message and get their values?

例如,如果 .wsdl 消息是这样的:

For example, if the .wsdl message is this:

<wsdl:message name="theRequest">
      <wsdl:part name="username" type="xsd:string"/>
      <wsdl:part name="password" type="xsd:string"/>
      <wsdl:part name="someMsg"  type="xsd:string"/>
</wsdl:message>

我想获取 someMsg 值并将其保存到字符串变量中.

I want to get someMsg value and save it into a String variable.

我正在看这个:获取 SoapBody 元素值,但并没有真正理解很多.如果有人可以提供解释或任何类型的指南,将不胜感激!

I was looking at this: Get SoapBody Element value, but didn't really understand much. If someone could provide an explanation or any kind of guide it would be really appreciated!

推荐答案

创建客户端来处理 SOAP 消息和 web 服务 的正常方法是;从 .xsd 模式生成 bean,从 .wsdl 生成所有存根以调用 web 服务(在本例中为 例如,Java 可以使用 JAXWSJAXB).

The normal way to create a client to deal with SOAP messages and web service are; generate the beans from the .xsd schemas, and all the stubs from .wsdl to invoke the web service (in this case for Java for example could be using JAXWS and JAXB).

还要注意,通常 .wsdl 定义了服务,但是如果您询问如何解析请求,最好显示 .xsd.

Note also that typically .wsdl define the service, but If you ask how to parse a request is better to show the .xsd.

无论如何,您当然可以直接使用apache http客户端来调用网络服务来进行POST然后处理响应...但请注意,这不是处理来自 SOAP Web 服务的大量请求和响应的推荐方法,因为这样您就必须手动解析每个响应才能开展业务.假设这是您的情况,您可以执行与此类似的操作来处理您的 SOAP 消息(我使用 javax.xml.soap.SOAPMessage 因为您似乎想使用它类基于您在问题中提供的链接).

Anyway of course you can invoke a web service using directly and apache http client or so to make a POST and then process the response... but note that this is not a recommended way to deal with a lot of request and response from a SOAP web service because then you've to parse manually each response to make your business. Supposing that this is your case you can do something similar to this to process your SOAP message ( I use javax.xml.soap.SOAPMessage since seems that you want to use this class based on the links you put in the question).

例如,如果您收到一条 SOAP 消息,例如:

For example if you're receiving a SOAP message like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
    <theRequest>
        <username>user</username>
        <password>password</password>
        <someMsg>sooomeMessage</someMsg>
      </theRequest>
   </soapenv:Body>
</soapenv:Envelope>

您可以执行以下操作:

import java.io.FileInputStream;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        // inputStream with your SOAP content... for the 
        // test I use a fileInputStream pointing to a file
        // which contains the request showed below
        FileInputStream fis = new FileInputStream("/path/yourSOAPReq.xml");

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,fis);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");

        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }

}

根据评论进行

它也适用于 Java 8,现在我唯一的猜测是 FileInputStream 发生了一些事情.您可以尝试以下相同的代码,但从 String 而不是从 File 获取请求.

It works for me also in Java 8, right now my only guess is that something is happening with the FileInputStream. Can you try the follow code which is the same but get the request from a String instead from a File.

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPMessage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SOAPMessageTest {

    public static void main(String[] args) throws Exception {

        // create message factory
        MessageFactory mf = MessageFactory.newInstance();
        // headers for a SOAP message
        MimeHeaders header = new MimeHeaders();     
        header.addHeader("Content-Type", "text/xml");

        String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
         "<soapenv:Body>"+
           "<theRequest>"+
             "<username>user</username>"+
             "<password>password</password>"+
             "<someMsg>sooomeMessage</someMsg>"+
           "</theRequest>"+
          "</soapenv:Body>"+
        "</soapenv:Envelope>";
        InputStream is = new ByteArrayInputStream(request.getBytes());

        // create the SOAPMessage
        SOAPMessage soapMessage = mf.createMessage(header,is);
        // get the body
        SOAPBody soapBody = soapMessage.getSOAPBody();
        // find your node based on tag name
        NodeList nodes = soapBody.getElementsByTagName("someMsg");
        System.out.println(nodes.getClass().getName());
        // check if the node exists and get the value
        String someMsgContent = null;
        Node node = nodes.item(0);
        someMsgContent = node != null ? node.getTextContent() : "";

        System.out.println(someMsgContent);
    }
}

希望能帮到你,

这篇关于在 Java 中从 SOAP 消息中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆