2010-08-26 27 views
0

我試過了Axis 2.它在嘗試從Seibel解析我的WSDL時死亡了。我不想要任何需要容器(Metro)的東西。我想要做的就是解析並形成SOAP消息。我不希望他們代表我發送消息。我已經在使用HttpClient並且很滿意。用於形成和分析肥皂消息的Java庫

回答

1

SAAJ(SOAP with Attachments API for Java)就是這樣做的。

但是您應該考慮使用Web服務堆棧而不是手動處理SOAP消息。

3

推薦使用的StAX(用於XML流API)

參考: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源開關:

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

雖然通過循環的事件,我們需要做的第一件事就是與eventReader.nextEvent()得到一個XMLEvent。一般情況下我們只關心那些開始元素的事件,這是與線檢索:

event.isStartElement() 

這檢查,看看是否是我們所看到的是一個開放的標籤。例如:<name>Fenton</name>只有<name>部分是起始元素。因此,採取XML的片段,請撥打以下電話

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

結果字符串:名稱。要獲得字符串芬頓我們呼籲:event.asCharacters().getData()

現在有時一個元素將具有如下屬性:<name type="User">Fenton</name>。在這種情況下,部分type="User"是一個屬性,它可以提取:

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