XStream解析XML

据说XStream解析XML很不错,刚好要写一个关于XML的解析.所以弄个小列子. 天气热起来了.



比如要把几个这样的几个对象封装成XML:

先写一个Person类.关于人的一些属性:

import java.util.Date;
import java.util.ArrayList;


public class Person {

private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
private ArrayList friends = new ArrayList();
private byte[] bytes;
private boolean bool;

/**
* @return fax
*/
public PhoneNumber getFax() {
return fax;
}
/**
* @param fax 要设置的 fax
*/
public void setFax(PhoneNumber fax) {
this.fax = fax;
}
/**
* @return firstname
*/
public String getFirstname() {
return firstname;
}
/**
* @param firstname 要设置的 firstname
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @return lastname
*/
public String getLastname() {
return lastname;
}
/**
* @param lastname 要设置的 lastname
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
* @return phone
*/
public PhoneNumber getPhone() {
return phone;
}
/**
* @param phone 要设置的 phone
*/
public void setPhone(PhoneNumber phone) {
this.phone = phone;
}
/**
* @return friends
*/
public ArrayList getFriends() {
return friends;
}
/**
* @param friends 要设置的 friends
*/
public void setFriends(ArrayList friends) {
this.friends = friends;
}

public void addFirend(Firend firend){
this.friends.add(firend);
}

public Person(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}
/**
* @return bytes
*/
public byte[] getBytes() {
return bytes;
}
/**
* @param bytes 要设置的 bytes
*/
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
/**
* @return bool
*/
public boolean isBool() {
return bool;
}
/**
* @param bool 要设置的 bool
*/
public void setBool(boolean bool) {
this.bool = bool;
}

}


再写一个Firend类,描述Person类Firend属性:

public class Firend {

private String name;

private Date bir;

/**
* @return name
*/
public String getName() {
return name;
}

/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}

public Firend(String name,Date bir) {
super();
this.name = name;
this.bir = bir;
}

/**
* @return bir
*/
public Date getBir() {
return bir;
}

/**
* @param bir 要设置的 bir
*/
public void setBir(Date bir) {
this.bir = bir;
}

}

再写一个PhoneNumber类,描述Person类PhoneNumber属性:

public class PhoneNumber {
private int code;
private String number;
/**
* @return code
*/
public int getCode() {
return code;
}
/**
* @param code 要设置的 code
*/
public void setCode(int code) {
this.code = code;
}
/**
* @return number
*/
public String getNumber() {
return number;
}
/**
* @param number 要设置的 number
*/
public void setNumber(String number) {
this.number = number;
}
public PhoneNumber(int code, String number) {
super();
this.code = code;
this.number = number;
}

}

测试类test.java
与Digester相比,很明显,不需要自己写配置文件来描述XML节点中对应的调用方法,干净利索!


import java.util.Date;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根

// XStream xstream = new XStream();

XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

xstream.alias("person", Person.class);
// xstream.addImplicitCollection(Firend.class, "friends");
xstream.alias("phonenumber", PhoneNumber.class);


Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
joe.addFirend(new Firend("陈冠希",new Date()));
joe.addFirend(new Firend("张柏芝",new Date()));
joe.addFirend(new Firend("谢霆锋",new Date()));
                  joe.addFirend(new Firend("刘文娴",new Date()));
joe.setBytes("新陈代谢".getBytes());


String xml = xstream.toXML(joe);

Person joe2 =  (Person)xstream.fromXML(xml);

// Firend fir = (Firend)joe2.getFriends().get(0);
// System.err.println(fir.getBir());
System.err.println(new String(joe2.getBytes()));

System.out.println(xml);
}

}


运行,控制台:

新陈代谢
<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
  <friends>
    <Firend>
      <name>陈冠希</name>
      <bir>2008-04-18 15:47:13.515 CST</bir>
    </Firend>
    <Firend>
      <name>张柏芝</name>
      <bir>2008-04-18 15:47:13.515 CST</bir>
    </Firend>
    <Firend>
      <name>谢霆锋</name>
      <bir>2008-04-18 15:47:13.515 CST</bir>
    </Firend>
    <Firend>
      <name>刘文娴</name>
      <bir>2008-04-18 17:02:37.703 CST</bir>
    </Firend>
  </friends>
  <bytes>0MKzwrT60Ls=</bytes>
  <bool>false</bool>
</person>



解析该XML 其他类不变.Test main:
package parse;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根

// XStream xstream = new XStream();

XStream xstream = new XStream(new DomDriver()); // does not require XPP3
// library

xstream.alias("person", Person.class);
xstream.alias("Firend", Firend.class);
xstream.alias("", PhoneNumber.class);

try {
Person person = (Person)xstream.fromXML(new FileReader("c://Person.xml"));//可以写相对目录
List friends = person.getFriends();
for(int i = 0; i< friends.size();i++){
Firend firend = (Firend)friends.get(i);
System.out.println(firend.getName());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}

XML里其他tag value可以类似取出.
运行,控制台:
陈冠希
张柏芝
谢霆锋
刘文娴




你可能感兴趣的:(xml)