如何将xml绑定到bean

发布于 2024-09-19 17:08:04 字数 392 浏览 8 评论 0原文

在我的应用程序中,我通过 HTTP 使用一些 API,它以 xml 形式返回响应。我想自动将数据从 xml 绑定到 beans。

例如,将以下 xml: 绑定

<xml>
   <userid>123456</userid>
   <uuid>123456</uuid>
</xml>

到此 bean(可能需要注释的帮助)

class APIResponce implement Serializable{

private Integer userid;
private Integer uuid;
....
}

执行此操作最简单的方法是什么?

In my application i use some API via HTTP and it returns responces as xml. I want automaticaly bind data from xml to beans.

For example bind following xml:

<xml>
   <userid>123456</userid>
   <uuid>123456</uuid>
</xml>

to this bean (maybe with help of annotations)

class APIResponce implement Serializable{

private Integer userid;
private Integer uuid;
....
}

What the simplest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筑梦 2024-09-26 17:08:04

我同意使用 JAXB。由于 JAXB 是一个规范,您可以从多种实现中进行选择:

以下是使用 JAXB 的方法:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class APIResponce {

    private Integer userid; 
    private Integer uuid; 

}

与以下 Demo 类一起使用时:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(APIResponce.class);

        File xml = new File("input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        APIResponce api = (APIResponce) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(api, System.out);
    }
}

将生成以下 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <userid>123456</userid>
    <uuid>123456</uuid>
</xml>

I agree with using JAXB. As JAXB is a spec you can choose from multiple implementations:

Here is how you can do it with JAXB:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class APIResponce {

    private Integer userid; 
    private Integer uuid; 

}

When used with the follow Demo class:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(APIResponce.class);

        File xml = new File("input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        APIResponce api = (APIResponce) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(api, System.out);
    }
}

Will produce the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <userid>123456</userid>
    <uuid>123456</uuid>
</xml>
护你周全 2024-09-26 17:08:04

作为 Castor 和 JAXB 的替代方案,Apache 还有一个用于进行 XML 到对象绑定的项目:

Betwixt: http:// /commons.apache.org/betwixt/

As an alternative to Castor and JAXB, Apache also has a project for doing XML to object binding:

Betwixt: http://commons.apache.org/betwixt/

薄荷港 2024-09-26 17:08:04

过去,我使用 XMLBeans 将 XML 绑定到 Java 类型。它真的很容易使用。您首先必须使用 scomp 命令(或 Maven 插件等)将 xml 模式编译为 Java 类型,并在代码中使用这些类型。

有一个正在运行的代码示例 此处< /a>.

In the past I have used XMLBeans for binding XML to Java types. It's really easy to use. You first have to compile your xml schema into Java types using the scomp command (or maven plugin etc) and use the types in your code.

There is an example of code in action here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文