Xstream解析XML,包括对数组和List的处理

前端之家收集整理的这篇文章主要介绍了Xstream解析XML,包括对数组和List的处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用Xstream须要引入xstream-1.4.jar和xpp3-1.1.4c.jar

import com.thoughtworks.xstream.XStream;
import com.hikvision.bms.main.Person;
/**
 * 使用Xstream解析XML字符串 
 */
public class XstreamDemo {
	
	/**
	 * xml字符串解析为实体类对象 
	 */
	public void str2Java() {
		String str = "<person>"
				      + "<name>Jack</name>"
				      + "<age>11</age>"
				      + "<sex>男</sex>"
				      + "<telphone>110</telphone>"
				      + "<lovers>parents</lovers>"
				      + "<lovers>wife</lovers>"
				      + "<friends>"
				        + "<string>LiLei</string>"
				        + "<string>LuXi</string>"
				      + "</friends>"
				      + "<roles>"
				        + "<string>friend</string>"
				        + "<string>father</string>"
				        + "<string>doctor</string>"  //注意string首字母必须小写
				      + "</roles>"
				   + "</person>";
		XStream xStream = new XStream();
		xStream.alias("person",Person.class);
		xStream.alias("lovers",String.class);     //private List<String> lovers;是string类型
        xStream.addImplicitCollection(Person.class,"lovers");//隐式集合 隐藏lovers
		Person person = (Person) xStream.fromXML(str);
		outPutInfo(person);
	}
	
	/**
	 * 将XML文件解析成实体类对象 
	 */
	public void str2JavaFormXML() {
		
		XStream xStream = new XStream();
		xStream.alias("person","lovers");//隐式集合 隐藏lovers
		Person person = (Person)xStream.fromXML(XstreamDemo.class.getResourceAsStream("person.xml"));
		outPutInfo(person);
	}

	/**
	 * 实体类转换为xml字符串 
	 */
	public void java2XmlStr() {
		Person person = new Person();
		person.setName("Lucy");
		person.setAge(22);
		person.setSex("女");
		person.setTelphone("120");
		XStream xStream = new XStream();
		xStream.alias("Girl",Person.class);
		String xmlStr = xStream.toXML(person);
		System.out.println(xmlStr);
	}
	
	public void outPutInfo(Person person) {
		System.out.println("name:" + person.getName());
		System.out.println("sex:" + person.getSex());
		System.out.println("telphone:" + person.getTelphone());
		for (String lover : person.getLovers()) {
			System.out.print(lover + " ");
		}
		System.out.println();
		for (String friend : person.getFriends()) {
			System.out.print(friend + " ");
		}
		System.out.println();
		for (String role : person.getRoles()) {
			System.out.print(role + " ");
		}
	}
	
	/**
	 * XStream 的常用方法
     * xstream.omitField(mytest.class,"name");            定义某一个属性的值不进行xml序列化。
     * xstream.alias("cat",Cat.class);
     * xstream.aliasField("age",Cat.class,"mAge");       对某一个类和类的属性进行别名定义。
     * xstream.useAttributeFor(String.class);              对所有String类型的字段定义为属性tag显示
     * xstream.useAttributeFor("name".String.class);       对所有String类型的字段名成为name 定义为属性tag显示
	 */
	public static void main(String[] args) {
		XstreamDemo demo = new XstreamDemo();
		//demo.str2Java();
		//demo.java2XmlStr();
		demo.str2JavaFormXML();
	}
	
}

person.java

import java.util.List;

public class Person {
	
	private String name;
	private Integer age;
	private String sex;
	private String telphone;
	private List<String> lovers;
	private List<String> friends;
	private String[] roles;
         /**set和get方法,以下省略**/	
}


XML文件 person.xml 放置位置跟XstreamDemo.class同一个目录

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <name>Jack</name>
	<age>11</age>
	<sex>男</sex>
	<telphone>110</telphone>
	<lovers>parents</lovers>
	<lovers>wife</lovers>
	<friends>
		<string>LiLei</string>
		<string>LuXi</string>
	</friends>
	<roles>
		<string>friend</string>
		<string>father</string>
		<string>doctor</string>
	</roles>
</person>

猜你在找的XML相关文章