1. 程式人生 > >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",Person.class);
		xStream.alias("lovers",String.class);     //private List<String> lovers;是string型別
        xStream.addImplicitCollection(Person.class, "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>