jdk1.5中xml对象互转(xstream1.1.3)

内容不多说,就xstream几个api。主要是xstream设置别名 xstream.alias("account", Account.class);来回转时,都必须指定好对象。

之前找了jaxb在jdk1.5中的总是调试不能用,果断用了xstream用了一下还可以,主要是我的功能能实现。jdk1.6后用jaxb确实挺简单了,如果也遇到同样在jdk1.5的同志们,不可以的话可以直接用我以下的方法。这边只贴代码了,程序只有自己悟才可以。(注意,在xml中如果有节点没值,必须把节点去掉不然会出错。)

Test类的实现 

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Test {

	public static void main(String[] args) {
		 marshal();
		 unmarshal();
	}
	
	public static void marshal(){
		XStream xstream = new XStream();
		User user = new User();
		user.setName("张三");
		user.setPhone("123456789");
		Account acc = new Account();
		acc.setId("0001");
		acc.setName("张三");
		user.setAcc(acc);
		Address add=null;
		List<Address> adds = new ArrayList<Address>();
		for(int i=0;i<3;i++){
			add = new Address();
			add.setCity("厦门"+i);
			add.setCode("12345"+i);
			adds.add(add);
			
		}
		user.setAddresses(adds);
        xstream.alias("account", Account.class);
        xstream.alias("address", Address.class);
		xstream.alias("user", User.class);
        xstream.toXML(user, System.out);
	}
	
	public static void unmarshal(){
		XStream xstream = new XStream(new DomDriver());
	    xstream.alias("account", Account.class);
	    xstream.alias("address", Address.class);
		xstream.alias("user", User.class);
		try {
		    User user = new User();       
		    FileInputStream fis = new FileInputStream("E:/eclipse/workspace/demo2/src/main/java/user.txt");
		    user = (User) xstream.fromXML(fis, user);
		    System.out.println(user);
		  } catch (Exception ex) {
		           ex.printStackTrace();
		  }
	}
}


User类

import java.util.List;


public class User {

	private Account acc;
	private String name;
	private String phone;
	
	private List<Address> addresses;
	public Account getAcc() {
		return acc;
	}
	public void setAcc(Account acc) {
		this.acc = acc;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public List<Address> getAddresses() {
		return addresses;
	}
	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}
	
	
	
}

Account类

public class Account {
	private String id;
	private String name;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Address类

public class Address {

	private String city;
	private String code;
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	
}

user.txt文本

<user>
  <acc>
    <id>0001</id>
    <name>张三</name>
  </acc>
  <name>张三</name>
  <phone>123456789</phone>
  <addresses>
    <address>
      <city>厦门0</city>
      <code>123450</code>
    </address>
    <address>
      <city>厦门1</city>
      <code>123451</code>
    </address>
    <address>
      <city>厦门2</city>
      <code>123452</code>
    </address>
  </addresses>
</user>


就上面这些内容了,我测试过了。jdk是1.5版本了,如果是jdk1.6的就不用这样麻烦了,现在java已经把jaxb整合到jdk的api库中。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值