2016-06-15 56 views
0

我從soap webservice獲取xml響應,但是當我解組時,它變爲null.when我將它打印在控制檯上時它正確打印,但是當我解組時它打印出null。通過SOAPMessage解析響應變爲空

下面是獲取響應

private static void printSOAPResponse(SOAPMessage soapResponse) 
    { 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
     XMLInputFactory xif = XMLInputFactory.newFactory(); 
     //StreamSource xml = new StreamSource(soapResponse.toString()); 
     XMLStreamReader xsr = xif.createXMLStreamReader(sourceContent); 
     xsr.nextTag(); 
     while(!xsr.getLocalName().equals("HelpDesk_Query_ServiceResponse")) { 
      xsr.nextTag(); 
     } 

    JAXBContext jc = JAXBContext.newInstance(HelpDesk_Query_ServiceResponse.class); 
    Unmarshaller unmarshaller = jc.createUnmarshaller(); 
    JAXBElement<HelpDesk_Query_ServiceResponse> jb = unmarshaller.unmarshal(xsr, HelpDesk_Query_ServiceResponse.class); 
    xsr.close(); 
    HelpDesk_Query_ServiceResponse response = jb.getValue(); 

    System.out.println(response.City); 
    System.out.println(response.Organization); 
} 

下面之後和解組的代碼被我的Java POJO類

@XmlAccessorType(XmlAccessType.FIELD) 
public class HelpDesk_Query_ServiceResponse { 
    @XmlAttribute 
    String Assigned_Group; 
    @XmlAttribute 
    String Organization; 
    @XmlAttribute 
    String City; 
//other attribute and their Getter and setter 
} 

此行transformer.transform(sourceContent,結果);是印刷這樣

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
    <ns0:HelpDesk_Query_ServiceResponse xmlns:ns0="urn:XXXX_HPD_IncidentInterface_WS__XXXXX" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <ns0:Assigned_Group>TIM-CSDWINDOWS-ADMIN</ns0:Assigned_Group> 
     <ns0:Assigned_Group_Shift_Name/> 
     <ns0:City>HYDERABAD</ns0:City> 
     <ns0:Organization>XXX_TIM</ns0:Organization> 
     <ns0:Priority>Medium</ns0:Priority> 
     <ns0:Product_Model_Version/> 
     <ns0:Product_Name/> 
     <ns0:HPD_CI_ReconID/> 
     <ns0:z1D_CI_FormName/> 
    </ns0:HelpDesk_Query_ServiceResponse> 
</soapenv:Body> 

,但是當我解組,並試圖打印城市和組織價值它打印空控制檯響應。 請有人幫助我。

回答

0

你有兩個問題,命名空間和使用屬性,而不是元素:

基本上,你應該使用:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(namespace="urn:XXXX_HPD_IncidentInterface_WS__XXXXX") 
public class HelpDesk_Query_ServiceResponse { 
    @XmlElement(namespace="urn:XXXX_HPD_IncidentInterface_WS__XXXXX") 
    String Assigned_Group; 
    @XmlElement(namespace="urn:XXXX_HPD_IncidentInterface_WS__XXXXX") 
    String Organization; 
    @XmlElement(namespace="urn:XXXX_HPD_IncidentInterface_WS__XXXXX") 
    String City; 
//other attribute and their Getter and setter 
}