XStream将XML映射到对对象



<?xml version="1.0"  encoding="UTF-8"?>
<address-book>

<contacts>
    <contact id="01" type="家庭" >
        <name>张三</name>
        <address>黄山路666号</address>
        <city>阜阳</city>
        <province>安徽</province>
        <postalcode>236000</postalcode>
        <country>中国</country>
        <telephone>18056075816</telephone>
</contact>
<contact id="02" type="商务" >
        <name>李四</name>
        <address>望江西路888号</address>
        <city>合肥</city>
        <province>安徽</province>
        <postalcode>230091</postalcode>
        <country>中国</country>
        <telephone>13956921922</telephone>
</contact>
<contact id="03" type="同学" >
        <name>王五</name>
        <address>民主路3号</address>
        <city>贵港市</city>
        <province>广西</province>
        <postalcode>537111</postalcode>
        <country>中国</country>
        <telephone>13965131384</telephone>
</contact>

</contacts>
</address-book>


这样格式的xml我相信大家都会读写,然而如果是下面这种情况呢?

<?xml version="1.0"  encoding="UTF-8"?>
<address-book>
    <contact id="01" type="家庭" >
        <name>张三</name>
        <address>黄山路666号</address>
        <city>阜阳</city>
        <province>安徽</province>
        <postalcode>236000</postalcode>
        <country>中国</country>
        <telephone>18056075816</telephone>
</contact>
<contact id="02" type="商务" >
        <name>李四</name>
        <address>望江西路888号</address>
        <city>合肥</city>
        <province>安徽</province>
        <postalcode>230091</postalcode>
        <country>中国</country>
        <telephone>13956921922</telephone>
</contact>
<contact id="03" type="同学" >
        <name>王五</name>
        <address>民主路3号</address>
        <city>贵港市</city>
        <province>广西</province>
        <postalcode>537111</postalcode>
        <country>中国</country>
        <telephone>13965131384</telephone>
</contact>
</address-book>
下面我将给出具体的读写和修改的代码:

新建一个Contact.class

import java.io.Serializable;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

public class Contact implements Serializable{
 @XStreamAsAttribute
 private String id;//xml中的属性需要添加注释
 @XStreamAsAttribute
 private String type;//xml中的属性需要添加注释
 private String name;
 private String address;
 private String city;
 private String province;
 private String postalcode;
 private String country;
 private String telephone;

 public String getId() {
  return id;
 }

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

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public String getName() {
  return name;
 }

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

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getProvince() {
  return province;
 }

 public void setProvince(String province) {
  this.province = province;
 }

 public String getPostalcode() {
  return postalcode;
 }

 public void setPostalcode(String postalcode) {
  this.postalcode = postalcode;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getTelephone() {
  return telephone;
 }

 public void setTelephone(String telephone) {
  this.telephone = telephone;
 }

 @Override
 public String toString() {
  return "id=" + id + ", type=" + type + ", name=" + name + ", address="
    + address + ", city=" + city + ", province=" + province
    + ", postalcode=" + postalcode + ", country=" + country
    + ", telephone=" + telephone;
 }

}

新建一个Address.class

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias(value="address-book")
public class Address {
@XStreamImplicit(itemFieldName="contact")
private List<Contact> address;

public List<Contact> getAddress() {
 return address;
}

public void setAddress(List<Contact> address) {
 this.address = address;
}

}

新建一个Test.class

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Scanner;

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

public class Test {
public static void main(String[] args) {
 Test t=new Test();
 Address address=t.readXml();
 System.out.println("输入id编号,从XML文件中查找联系人,联系人信息:");
 Scanner sc=new Scanner(System.in);
 String line=sc.nextLine();
 List<Contact> list=address.getAddress();
 for (Contact contact : list) {
  if(contact.getId().equals(line)){
   System.out.println(contact);
  }
 }
 System.out.println("将编号为02的联系人的姓名和类型修改为用户输入的信息(格式如:赵六,单位),并保存到XML文件中。");
 String info=sc.nextLine();
 Address add=t.paresInfo(address,info);
 t.write(add);
}
private  Address paresInfo(Address address, String info) {
 String[] infos=info.split(",");
 List<Contact> list=address.getAddress();
 for (Contact contact : list) {
  if(contact.getId().equals("02")){
   contact.setName(infos[0]);
   contact.setType(infos[1]);
  }
 }
 return address;
 
}
private  void write(Address address) {
 
 try {
  XStream xs=new XStream(new DomDriver());
  xs.processAnnotations(Address.class);
  OutputStreamWriter osw=new FileWriter(new File("a.xml"));
  xs.toXML(address, osw);
 } catch (IOException e) {
  e.printStackTrace();
 }
}
public Address readXml(){
 XStream xs=new XStream(new DomDriver());
 xs.processAnnotations(Address.class);
 InputStreamReader isr=null;
 try {
  isr = new FileReader(new File("E:\\workspace\\Day24\\src\\PeopleMessage.xml"));
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 Address address=(Address) xs.fromXML(isr);
 String xml=xs.toXML(address);
 System.out.println(xml);
 return address;  
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值