如何将xml绑定到bean

时间:2022-02-25 21:49:58

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

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

For example bind following xml:

例如,绑定xml:

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

to this bean (maybe with help of annotations)

到这个bean(也许借助于注释)

class APIResponce implement Serializable{

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

What the simplest way to do this?

最简单的方法是什么?

4 个解决方案

#1


5  

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

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

Here is how you can do it with 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; 

}

When used with the follow Demo class:

与以下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);
    }
}

Will produce the following XML:

将生成以下XML:

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

#2


4  

Try JAXB - http://jaxb.java.net/

试试JAXB - http://jaxb.java.net/

An intro article for it http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

一篇介绍它的文章http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

#3


1  

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

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

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

#4


1  

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.

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

There is an example of code in action here.

这里有一个代码实例。

#1


5  

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

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

Here is how you can do it with 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; 

}

When used with the follow Demo class:

与以下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);
    }
}

Will produce the following XML:

将生成以下XML:

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

#2


4  

Try JAXB - http://jaxb.java.net/

试试JAXB - http://jaxb.java.net/

An intro article for it http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

一篇介绍它的文章http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html

#3


1  

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

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

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

#4


1  

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.

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

There is an example of code in action here.

这里有一个代码实例。