当前位置: 首页 > 面试题库 >

将Soap XML响应转换为对象

乌学博
2023-03-14
问题内容

我是使用SOAP API的新手

我有一个来自API的肥皂响应

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<LoginResponse xmlns="http://test.org/ADMail_Service">
<LoginResult>
<ErrorMessage>Successful login</ErrorMessage>
<Status>true</Status>
</LoginResult>
</LoginResponse>
</soapenv:Body>
</soapenv:Envelope>

我正在尝试将其转换为对象。

通过在线阅读文章,我试图使用JAXB来做到这一点,但是我的对象是空的。

这是读取响应的代码。我将响应写到xml文件中以进行测试:

try {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to Envelope tag

    xsr.nextTag(); // Advance to Body tag
    xsr.nextTag();
    xsr.nextTag();


    JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

    System.out.println(je.getName());
    System.out.println(je.getValue());
} catch (XMLStreamException e) {
    e.printStackTrace();
} catch (JAXBException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

LoginResult类:

public class LoginResult {
    private String errorMessage;
    private String status;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

提前致谢!


问题答案:

您可以使用此代码检索POJO,还可以将@XmlRootElement作为标头添加到POJO。

(我没有测试下面的代码)

XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
        xsr.nextTag(); // Advance to Envelope tag

        xsr.nextTag(); // Advance to Body tag
        xsr.nextTag();
        xsr.nextTag();

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new StAXSource(xsr), new StreamResult(stringWriter));
        StringReader sr = new StringReader(stringWriter.toString());
        JAXBContext jaxbContext = JAXBContext.newInstance(LoginResult.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        LoginResult loginResult = (LoginResult) unmarshaller.unmarshal(sr);

编辑:

我为您找到了解决方案:

    @XmlRootElement(name = "LoginResult", namespace = "http://test.org/ADMail_Service")
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginResult {
    @XmlElement(name = "ErrorMessage", namespace = "http://test.org/ADMail_Service")
    private String errorMessage;
    @XmlElement(name = "Status", namespace = "http://test.org/ADMail_Service")
    private String status;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


public static void main(String[] args) {
        try {
            XMLInputFactory xif = XMLInputFactory.newFactory();
            XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
            xsr.nextTag(); // Advance to Envelope tag

            xsr.nextTag(); // Advance to Body tag
            xsr.nextTag();
            xsr.nextTag();


            JAXBContext jc = JAXBContext.newInstance(LoginResult.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            JAXBElement<LoginResult> je = unmarshaller.unmarshal(xsr, LoginResult.class);

            System.out.println(je.getName());
            System.out.println(je.getValue());
            System.out.println(je.getValue().getErrorMessage());
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }


 类似资料:
  • 我正在处理jBPM 7.22.0。最终的 我使用服务任务REST从这个api获取响应https://reqres.in/api/users/1 我想将此响应获取到一个对象中,因此我创建了这个类(或jBPM中调用的数据对象): 我在jBPM中创建了一个名为res的全局变量,该变量具有我要存储响应的数据对象的类型。因此,在我的REST服务任务的“任务”中,我有屏幕截图中的内容。 如您所见,我想将结果存

  • 我需要将其转换为以下格式: 类型的数量可以改变(例如,可以只有A和B)。有人能帮我吗?我使用这个组件在网站https://js.devexpress.com/demos/widgetsgallery/demo/datagrid/simplearray/angular/light/上显示数据

  • 我有一个带有POST REST API的SpringBoot2.2 webservice。我正在使用Okhttp客户机向第三方服务发出请求。我希望将第三方服务的确切响应返回给我的WebService的调用者。所有okhttp食谱都参考: 我尝试在api中返回okhttp响应,但我只返回调用方示例: 有人知道如何将Okhttp响应对象转换为Spring ResponseEntity对象以便返回htt

  • 我是格森的新手。我需要将下面的JSON响应转换成一个列表。 JSON响应: 我有一门课要教数据 账户JAVA 当我对我的班级做出回应时,我得到了: 现在我需要将这两个值放入

  • 我用这个服务结果构造了一个组件类对象,我也得到了错误 错误TypeError:无法读取未定义的属性“name” 当我在HTML文件中使用它时。

  • 我有一个问题,我已经在stackoverflow上问了几次,我已经尝试了所有这些问题,没有一个有效。所以我很想在另一个时间总结这个问题,并试着更准确地描述它。 我正在构建一个应用程序,将图片发送到python后端,以获得xcode swift中的图像识别结果。 我正在使用Alamofire上传,这里是上传部分: 下面是我从服务器端得到的json响应: 所以,我想要达到的目的,就是打印第一个预言的名