前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XStream将XML映射到对对象

XStream将XML映射到对对象

作者头像
用户1171305
发布2017-12-28 11:05:40
9250
发布2017-12-28 11:05:40
举报
文章被收录于专栏:成长道路

<?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;   } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-08-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档