用于形成和解析soap消息的Java库 [英] Java library to form and parse soap messages

查看:19
本文介绍了用于形成和解析soap消息的Java库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过 Axis 2.它在尝试从 Seibel 解析我的 WSDL 时死了.我不想要任何需要容器(地铁)的东西.我想要做的就是解析和形成 SOAP 消息.我不希望他们代表我发送消息.为此,我已经在使用 HttpClient 并且对此感到满意.

I've tried Axis 2. It dies when trying to parse my WSDL from Seibel. I don't want anything that requires a container (Metro). All I want to do is parse and form SOAP messages. I don't want them sending the messages on my behalf. I'm already using HttpClient for that and am happy with it.

推荐答案

推荐使用 StAX (Streaming API for XML)

Recommend using StAX (Streaming API for XML)

参考:http://www.vogella.de/articles/JavaXML/article.html示例 XML

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SOAP-ENV:Body>
    <ns:PicklistWS_GetPicklistValues_Output xmlns:ns="urn:crmondemand/ws/picklist/">
      <ListOfParentPicklistValue xmlns="urn:/crmondemand/xml/picklist">
        <ParentPicklistValue>
          <Language>ENU</Language>
          <ParentFieldName/>
          <ParentDisplayValue/>
          <ParentCode/>
          <Disabled/>
          <ListOfPicklistValue>
            <PicklistValue>
              <Code>F</Code>
              <DisplayValue>F</DisplayValue>
              <Disabled>N</Disabled>
            </PicklistValue>
            <PicklistValue>
              <Code>M</Code>
              <DisplayValue>M</DisplayValue>
              <Disabled>N</Disabled>
            </PicklistValue>
          </ListOfPicklistValue>
        </ParentPicklistValue>
      </ListOfParentPicklistValue>
    </ns:PicklistWS_GetPicklistValues_Output>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

解析代码:

static Map<String, String> getPicklistFromSoapResponse(String soapResponse) throws ServiceException {
    Map<String, String> values = new LinkedHashMap<String, String>();
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    String code = null;
    String display = null;
    String disabled = null;
    try {
        InputStream in = new ByteArrayInputStream(soapResponse.getBytes("UTF-8"));
        XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                if (event.asStartElement().getName().getLocalPart().equals("Code")) {
                    event = eventReader.nextEvent();
                    code = event.asCharacters().getData();
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("DisplayValue")) {
                    event = eventReader.nextEvent();
                    display = event.asCharacters().getData();
                    continue;
                }
                if (event.asStartElement().getName().getLocalPart().equals("Disabled")) {
                    event = eventReader.nextEvent();
                    disabled = event.asCharacters().getData();
                    if ( "Y".equals(disabled)) values.put(code, display);
                    continue;
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new ServiceException(e);
    } catch (XMLStreamException e) {
        throw new ServiceException(e);
    }
    return values;
}

行:

InputStream in = new ByteArrayInputStream(soapResponse.getBytes("UTF-8"));

可以切换一个文件作为xml的来源:

can be switch for a file as the source of the xml with:

InputStream in = new FileInputStream("myFile.xml");

在循环遍历事件时,我们需要做的第一件事是使用 eventReader.nextEvent() 获取 XMLEvent.通常我们只关心作为开始元素的事件,它用以下行检索:

While looping through the events, the first thing we need to do is get the XMLEvent with eventReader.nextEvent(). Normally we only care for events that are start elements, which is retrieved with the line:

event.isStartElement()

这会检查我们看到的是否是开始标签.例如在:Fenton 中,只有 部分是开始元素.所以拿那个xml片段,调用以下

This checks to see if what we are looking at is an opening tag. For example in: <name>Fenton</name> only the <name> part is the start element. So taking that snippet of xml, calling the following

event.asStartElement().getName().getLocalPart()

结果是字符串:name.要获取我们调用的字符串 Fenton:event.asCharacters().getData()

results in the string: name. To get the string Fenton we call: event.asCharacters().getData()

现在有时一个元素会具有类似的属性:Fenton.在这种情况下,部分 type="User" 是一个属性,可以通过以下方式提取:

Now sometimes an element will have attributes like: <name type="User">Fenton</name>. In this case the part type="User" is an attribute and it can be extracted with:

StartElement startElement = event.asStartElement();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
    Attribute attribute = attributes.next();
    if (attribute.getName().toString().equals("type"));
    String typeIsUser = attribute.getValue();
}

这篇关于用于形成和解析soap消息的Java库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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