Binding XML using POJO and JAXB annotations(使用 POJO 和 JAXB 注释绑定 XML)
问题描述
我有以下 xml 格式,我想通过 POJO 并使用 JAXB 注释来绑定它.XML 格式如下:
I have the following xml format that i want to bind it through a POJO and using JAXB annotations. The XML format is the following:
<datas>
<data>apple<data>
<data>banana<data>
<data>orange<data>
<datas>
我正在尝试通过以下 POJO 绑定数据:
And i'm trying to bind the data through the following POJO:
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
@XmlElement
private List<String> data;
//get/set methods
}
我也尝试过这个 POJO:
And also i try and this POJO:
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
@XmlElement
private List<Data> datas;
//get/set methods
}
//
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Data{
@XmlElement
private String data;
//get/set methods
}
在第一种情况下,它只检索第一个数据:apple.在第二种情况下不检索任何东西.有人可以帮我提供适当的 POJO 和注释以绑定所有数据吗?
In the first case it retrieves only the first data: apple. In the second case doesn't retrieve anything. Could someone help me to provide the appropriate POJO and annotations in order to bind all data?
推荐答案
您可以执行以下选项之一:
You can do one of the following options:
选项 #1
数据
package forum11311374;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
private List<String> data;
//get/set methods
}
更多信息
- http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
选项 #2
数据
package forum11311374;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Datas {
@XmlElement(name="data")
private List<Data> datas;
//get/set methods
}
数据
package forum11311374;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Data{
@XmlValue
private String data;
//get/set methods
}
更多信息
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
以下两个选项都可以使用:
The following can be used with both options:
input.xml/输出
我已更新 XML 文档以包含必要的结束标记.<data>apple</data>
而不是 <data>apple.
I have updated the XML document to contain the necessary closing tags. <data>apple</data>
instead of <data>apple<data>
.
<datas>
<data>apple</data>
<data>banana</data>
<data>orange</data>
</datas>
演示
package forum11311374;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Datas.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11311374/input.xml");
Datas datas = (Datas) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(datas, System.out);
}
}
这篇关于使用 POJO 和 JAXB 注释绑定 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 POJO 和 JAXB 注释绑定 XML


- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01