使用 XML 工具: xstream
2024年4月14日大约 1 分钟
Xstream 是一种 OXMapping 技术,是用来处理 XML 文件序列化的框架。
JDK jaxb 的封装。
官网: https://x-stream.github.io/tutorial.html
https://github.com/LawssssCat/blog/tree/master/src/zh/dev-java-xml/code/demo-java-xml/n13-springoxm-usage/test/java/org/example/编程式
::: tabs
@tab 测试类
File not found@tab 测试类(抽象)
File not found@tab 实体类
File not found:::
注解式
::: tabs
@tab 测试类
File not found@tab 实体类
File not found:::
自定义转换器
::: tabs
@tab 转换器
File not found@tab 测试类
File not found:::
转换 JSON
添加依赖
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.5.4</version>
</dependency>::: tabs
@tab 测试类
File not found@tab 输出
{
"person": {
"@age": 18,
"姓名": "steven",
"sites": [
{
"site": [
{ "id": 111, "url": "http://n1.example.org" },
{ "id": 222, "url": "https://n2.example.org" }
]
}
]
}
}:::
对象流
::: tabs
@tab 测试类
File not found@tab 输出
<object-stream>
<person age="18">
<姓名>steven</姓名>
<sites>
<site>
<id>111</id>
<url>http://n1.example.org</url>
</site>
<site>
<id>222</id>
<url>https://n2.example.org</url>
</site>
</sites>
</person>
<person age="18">
<姓名>steven</姓名>
<sites>
<site>
<id>111</id>
<url>http://n1.example.org</url>
</site>
<site>
<id>222</id>
<url>https://n2.example.org</url>
</site>
</sites>
</person>
<string>hello world!</string>
<int>1024</int>
</object-stream>:::
对象持久化
持久化后,会生成 int@0.xml、int@1.xml、int@2.xml 这样的文件。这些文件内容就是数组里的内容。
::: tabs
@tab 测试类
File not found:::