通过 SOAPMessage 响应解析得到 null

Parsing through SOAPMessage response getting null

我从 soap webservice 得到 xml 响应但是当我解组它时我得到 null.when 我在控制台上打印它打印正确但是当我解组它打印空。

下面是收到响应后解组的代码

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 class

@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, result); 在控制台中像这样打印响应

<?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>

但是当我解组并尝试打印城市和组织值时,它打印为空。 请有人帮助我。

你有两个问题,命名空间和使用属性而不是元素:

基本上,你应该使用:

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