Java library to form and parse soap messages

怎甘沉沦 提交于 2019-12-23 04:48:19

问题


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.


回答1:


Recommend using StAX (Streaming API for XML)

reference: http://www.vogella.de/articles/JavaXML/article.html Sample 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>

Parsing Code:

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;
}

The line:

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

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

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

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()

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()

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

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();
}



回答2:


SAAJ (SOAP with Attachments API for Java) does that.

But you should consider using a web-service stack rather than manually handling SOAP messages.



来源:https://stackoverflow.com/questions/3571764/java-library-to-form-and-parse-soap-messages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!