前言:日常开发过程中,经常会遇到需要将Java 对象转XML 或者将XML 转Java 对象,本篇是作者总结日常研发过程中Java与XML相互转换的方法,仅供参考。
一、Java封装的XML API
JAXB(Java Architecture for XML Binding简称JAXB) :允许Java开发人员将Java类映射为XML表示方式。JAXB提供两种主要特性:将一个Java对象序列化为XML,以及反向操作,将XML解析成Java对象。换句话说,JAXB允许以XML格式存储和读取数据,而不需要程序的类结构实现特定的读取XML和保存XML的代码。——摘自 OSChina 《JAXB Java 的 XML 开发包》
-
编写XML 工具类 import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
public class XMLUtils {
public static <T> String beanToXmlStr(T bean, String charset) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
StringWriter writer = new StringWriter();
marshaller.marshal(bean, writer);
return writer.toString();
}
public static <T> T xmlStrToBean(String xml, Class<T> clazz) throws JAXBException {
JAXBContext jaxbContextResp = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContextResp.createUnmarshaller();
StringReader reader = new StringReader(xml);
return (T) unmarshaller.unmarshal(reader);
}
}
-
编写Java Bean import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Data
@XmlRootElement(name = "Person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Age")
private Integer age;
}
-
测试 import com.test.bean.Person;
import org.junit.Test;
import javax.xml.bind.JAXBException;
public class TestXml {
@Test
public void test() throws JAXBException {
Person person = new Person();
person.setName("Tom");
person.setAge(20);
String xmlStr = XMLUtils.beanToXmlStr(person, "UTF-8");
System.out.println(xmlStr);
Person person2 = XMLUtils.xmlStrToBean(xmlStr, Person.class);
System.out.println(person2);
}
}
-
运行结果截图 -
详细资料请查阅: JAXB教程–入门 JAXB Java 的 XML 开发包
二、XStream
XStream 是一个Java 对象和XML 相互转换的工具,很好很强大。提供了所有的基础类型、数组、集合等类型直接转换的支持。因此XML 常用于数据交换、对象序列化。——摘自 OSChina 《XStream XML 操作类库》
- 导入依赖
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
- 编写
XStream 工具类import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.naming.NoNameCoder;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XStreamUtil {
public static <T> T xmlStrToBean(String xml, Class<T> clazz) {
XStream stream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
stream.processAnnotations(clazz);
stream.autodetectAnnotations(true);
return (T) stream.fromXML(xml);
}
public static <T> String beanToXmlStr(T bean) {
XStream stream = new XStream(new DomDriver("UTF-8", new NoNameCoder()));
stream.autodetectAnnotations(true);
return stream.toXML(bean);
}
}
- 编写
Java Bean import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
@Getter
@Setter
@ToString
@XStreamAlias("Student")
public class Student {
@XStreamAlias("Name")
private String name;
@XStreamAlias("Sex")
private String sex;
@XStreamAlias("Age")
private String age;
@XStreamAlias("NickName")
private String nickName;
@XStreamAlias("IsSuccess")
private Boolean isSuccess;
@XStreamAlias("BirthDay")
private Date birthDay;
}
- 编写测试方法
public class TestXStream {
@Test
public void test() {
String xmlStr = "<Student>\n" +
" <Name>mingo</Name>\n" +
" <Sex>男</Sex>\n" +
" <NickName>luffy</NickName>\n" +
" <IsSuccess>true</IsSuccess>\n" +
" <BirthDay>1991-12-01 00:00:00</BirthDay>\n" +
"</Student>";
Student student = XStreamUtil.xmlStrToBean(xmlStr, Student.class);
System.out.println(student);
String str = XStreamUtil.beanToXmlStr(student);
System.out.println(str);
}
}
- 运行结果截图
- 详细资料请查阅:XStream XML 操作类库
|