最近用xml的数据格式比较多,所以简单记录一下这些类库的使用。
dom4j用dom模型的方式解析xml数据。
<dependency>
<groupId>org.dom4jgroupId>
<artifactId>dom4jartifactId>
<version>2.1.3version>
dependency>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italiantitle>
<author>Giada De Laurentiisauthor>
<year>2005year>
<price>30.00price>
book>
<book category="CHILDREN">
<title lang="en">Harry Pottertitle>
<author>J K. Rowlingauthor>
<year>2005year>
<price>29.99price>
book>
<book category="WEB">
<title lang="en">XQuery Kick Starttitle>
<author>James McGovernauthor>
<author>Per Bothnerauthor>
<author>Kurt Cagleauthor>
<author>James Linnauthor>
<author>Vaidyanathan Nagarajanauthor>
<year>2003year>
<price>49.99price>
book>
<book category="WEB">
<title lang="en">Learning XMLtitle>
<author>Erik T. Rayauthor>
<year>2003year>
<price>39.95price>
book>
bookstore>
@Test
public void testDom4j() throws DocumentException, IOException {
//获取文件对象
File file=new File("book.xml");
//创建解析器
SAXReader reader = new SAXReader();
//通过解析器获取文件内容,返回Document对象
Document document = reader.read(file);
//通过Document对象获取节点
Element rootElement = document.getRootElement();
//获取节点名
System.out.println(rootElement.getName());
//将节点对象转换为xml字符串
System.out.println(rootElement.asXML());
}
Xstream通过反射,将JavaBean个xml进行互相转换,也可以进行JavaBean和Json数据之间的转换。
<dependency>
<groupId>com.thoughtworks.xstreamgroupId>
<artifactId>xstreamartifactId>
<version>1.4.11.1version>
dependency>
<dependency>
<groupId>org.codehaus.jettisongroupId>
<artifactId>jettisonartifactId>
<version>1.4.1version>
dependency>
xml和javaBean之间转换
@Data
@AllArgsConstructor
public class User {
private String name;
private Integer id;
private String sex;
private Integer age;
}
@Data
@AllArgsConstructor
public class Person {
private String name;
private Integer id;
private String sex;
}
@Test
public void testX(){
//使用Stax不需要加入其他jar包做驱动
XStream stream = new XStream(new StaxDriver());
//给根节点起别名,默认是类名
stream.alias("users",User.class);
//给字段起别名
stream.aliasField("姓名",User.class,"name");
//将bean转换为xml格式
String s = stream.toXML(new User("Jack",12666,"男",45));
System.out.println(s);
//从xml格式中获取对象
System.out.println(stream.fromXML(s));
//将bean以xml方式序列化到输出流中
stream.toXML(new User("Jack",12666,"男",45),new FileWriter(new File("user.xml")));
}
<?xml version="1.0" ?><users><姓名>Jack</姓名><id>12666</id><sex>男</sex><age>45</age></users>
Security framework of XStream not initialized, XStream is probably vulnerable.
User(name=Jack, id=12666, sex=男, age=45)
json和javabean之间转换,有两种驱动选择:
//json驱动,自带的,只能写不能读
XStream stream = new XStream(new JsonHierarchicalStreamDriver());
stream.allowTypes(new Class[]{
User.class});
String xml = stream.toXML(new User("Jack", 12666, "男", 45));
System.out.println(xml);
//如其名字,分层显示
{
"com.cgl.ek.pojo.User": {
"name": "Jack",
"id": 12666,
"sex": "男",
"age": 45
}}
//内部实现一个方法,去掉根节点
XStream streamJson = new XStream(new JsonHierarchicalStreamDriver(){
public HierarchicalStreamWriter createWriter(Writer out) {
return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
}
});
String xmlJson = streamJson.toXML(new User("Jack", 12666, "男", 45));
System.out.println(xmlJson);
//没有根节点
{
"name": "Jack",
"id": 12666,
"sex": "男",
"age": 45
}
//从json字符串中还原对象,需要json包
XStream jStream = new XStream(new JettisonMappedXmlDriver());
Object o = jStream.fromXML(xml);
String toXML = jStream.toXML(new User("Jack", 12666, "男", 45));
System.out.println(toXML);
{
"com.cgl.ek.pojo.User":{
"name":"Jack","id":12666,"sex":"男","age":45}}
XStream jnStream = new XStream(new JettisonMappedXmlDriver());
//jnStream.alias("user",User.class);
jnStream.setMode(XStream.ID_REFERENCES);//这里可以设置模式
String toXML2 = jnStream.toXML(new User("Jack", 12666, "男", 45));
System.out.println(toXML2);
//多出一个@ID字段
{
"com.cgl.ek.pojo.User":{
"@id":1,"name":"Jack","id":12666,"sex":"男","age":45}}
这个库用来进行对象之间的拷贝,像BeanUtils,不同的是使用字节码生成数据。
@Test
public void testBean(){
//获取映射工厂
DefaultMapperFactory.Builder builder = new DefaultMapperFactory.Builder();
DefaultMapperFactory factory = builder.build();
//User中的数据拷贝到Person中,字段对应,剩下的用默认,最后注册
factory.classMap(User.class, Person.class).field("name","name").byDefault().register();
User u=new User("Jack",12666,"男",45);
//映射装饰
MapperFacade facade = factory.getMapperFacade();
//开始拷贝
Person person = facade.map(u, Person.class);
System.out.println(person);
}
//没有的字段不会映射过去
Person(name=Jack, id=12666, sex=男)