XStream 是一个简单的基于 Java 库,Java 对象序列化到 XML,反之亦然 (即:可以轻易的将 Java 对象和 xml 文档相互转换)。
通过它传递一个 StaxDriver 创建 XStream 对象。 StaxDriver 使用 SAX 解析器 (可从 Java6),一个快速的 XML 解析器。
XStream xstream = new XStream(new StaxDriver());
使用 toXML () 方法来获取对象的 XML 字符串表示。
String xml = xstream.toXML(student);
使用 fromXML () 方法来从 XML 对象。
Student student1 = (Student)xstream.fromXML(xml);
实现java对象转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">
- <parent>
- <artifactId>springboot-demo</artifactId>
- <groupId>com.et</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>xstream</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.thoughtworks.xstream</groupId>
- <artifactId>xstream</artifactId>
- <version>1.4.19</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
-
- </dependencies>
- </project>
- package com.et.xstream;
-
- import com.thoughtworks.xstream.XStream;
- import com.thoughtworks.xstream.io.xml.DomDriver;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.HashMap;
- import java.util.Map;
-
- @RestController
- public class HelloWorldController {
- @RequestMapping(value = "/hello" ,produces = {"application/xml;"})
- public String showHelloWorld(){
- Employee e1 = new Employee("Sanyog", "Gautam", 1000,
- 19, "Male");
-
- // Serializing a Java object into XML
- XStream xStream = new XStream(new DomDriver());
- String xml = xStream.toXML(e1); // Converting it to XML
-
- return xml;
- }
- }
- package com.et.xstream;
-
- import lombok.Getter;
-
- @Getter
- public class Employee {
- private String firstName;
- private String lastName;
- private int salary;
- private int age;
- private String gender;
-
- public Employee(String firstName, String lastName,
- int salary, int age, String gender)
- {
- this.firstName = firstName;
- this.lastName = lastName;
- this.salary = salary;
- this.age = age;
- this.gender = gender;
- }
- }
- package com.et.xstream;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class DemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
- }
以上只是一些关键代码,所有代码请参见下面代码仓库