“Jackson 反序列化XML”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) 小 (Jihongchang移动页面Jackson至Jackson 反序列化XML) |
Jihongchang(讨论 | 贡献) |
||
| 第1行: | 第1行: | ||
反序列化 XML | 反序列化 XML | ||
| − | pom.xml<syntaxhighlight lang="xml"> | + | === pom.xml === |
| + | <syntaxhighlight lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 第50行: | 第51行: | ||
</project> | </project> | ||
| − | </syntaxhighlight>users.xml<syntaxhighlight lang="xml"> | + | </syntaxhighlight> |
| + | |||
| + | === users.xml === | ||
| + | <syntaxhighlight lang="xml"> | ||
<users> | <users> | ||
<user id="1"> | <user id="1"> | ||
| 第61行: | 第65行: | ||
</user> | </user> | ||
</users> | </users> | ||
| − | </syntaxhighlight>UserList.java<syntaxhighlight lang="java"> | + | </syntaxhighlight> |
| + | |||
| + | === UserList.java === | ||
| + | <syntaxhighlight lang="java"> | ||
package org.example; | package org.example; | ||
| 第85行: | 第92行: | ||
} | } | ||
| − | </syntaxhighlight>User.java<syntaxhighlight lang="java"> | + | </syntaxhighlight> |
| + | |||
| + | === User.java === | ||
| + | <syntaxhighlight lang="java"> | ||
package org.example; | package org.example; | ||
| 第112行: | 第122行: | ||
| − | </syntaxhighlight>Xml2ObjectTest2.java<syntaxhighlight lang="java"> | + | </syntaxhighlight> |
| + | |||
| + | === Xml2ObjectTest2.java === | ||
| + | <syntaxhighlight lang="java"> | ||
package org.example; | package org.example; | ||
| 第118行: | 第131行: | ||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||
import java.io.File; | import java.io.File; | ||
| + | import java.io.FileInputStream; | ||
import java.io.IOException; | import java.io.IOException; | ||
| 第124行: | 第138行: | ||
@Test | @Test | ||
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException { | public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException { | ||
| + | |||
XmlMapper xmlMapper = new XmlMapper(); | XmlMapper xmlMapper = new XmlMapper(); | ||
| + | |||
| + | UserList userList; | ||
File file2 = new File("E:\\record\\2025\\12\\5\\users.xml"); | File file2 = new File("E:\\record\\2025\\12\\5\\users.xml"); | ||
| − | + | ||
| + | userList = xmlMapper.readValue(file2, UserList.class); | ||
System.out.println(userList); | System.out.println(userList); | ||
| + | |||
| + | FileInputStream fis = new FileInputStream(file2); | ||
| + | // 传入文件流 + 目标类型 | ||
| + | userList = xmlMapper.readValue(fis, UserList.class); | ||
| + | System.out.println(userList); | ||
| + | |||
} | } | ||
2025年12月5日 (五) 09:28的最新版本
反序列化 XML
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
users.xml
<users>
<user id="1">
<name>张三</name>
<age>25</age>
</user>
<user id="2">
<name>李四</name>
<age>30</age>
</user>
</users>
UserList.java
package org.example;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
// 根节点(可选,流式解析时可省略)
@Data
@NoArgsConstructor
@JacksonXmlRootElement(localName = "users")
public class UserList {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "user")
private List<User> users;
// 无参构造器 + getter/setter
}
User.java
package org.example;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import lombok.NoArgsConstructor;
// 单个用户节点
@Data
@NoArgsConstructor
@JacksonXmlRootElement(localName = "user")
public class User {
@JacksonXmlProperty(isAttribute = true) // 映射 XML 属性(id="1")
private Long id;
@JacksonXmlProperty // 映射 XML 子节点
private String name;
@JacksonXmlProperty
private Integer age;
// 无参构造器(Jackson 必须)+ getter/setter
// 省略 getter/setter
}
Xml2ObjectTest2.java
package org.example;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Xml2ObjectTest2 {
@Test
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
XmlMapper xmlMapper = new XmlMapper();
UserList userList;
File file2 = new File("E:\\record\\2025\\12\\5\\users.xml");
userList = xmlMapper.readValue(file2, UserList.class);
System.out.println(userList);
FileInputStream fis = new FileInputStream(file2);
// 传入文件流 + 目标类型
userList = xmlMapper.readValue(fis, UserList.class);
System.out.println(userList);
}
}