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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏软件开发 -- 分享 互助 成长

CRC校验码

循环冗余校验码(CRC)的基本原理是:在K位信息码后再拼接R位的校验码,整个编码长度为N位,因此,这种编码也叫(N,K)码。对于一个给定的(N,K)码,可以证明...

2246
来自专栏码匠的流水账

聊聊storm的OpaquePartitionedTridentSpoutExecutor

本文主要研究一下storm的OpaquePartitionedTridentSpoutExecutor

1133
来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

852
来自专栏悦思悦读

Spark Tips3: 在Spark Streaming job中读取Kafka messages及其offsetRange

在Spark Streaming job中读取Kafka topic(s)中的messages时,有时我们会需要同步记录下每次读取的messages的offse...

44212
来自专栏数据结构与算法

P2580 于是他错误的点名开始了

题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人。 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉...

2877
来自专栏noteless

[四] java8 函数式编程 收集器浅析 收集器Collector常用方法 运行原理 内部实现

收集器是由四个函数约定构成,它们一起工作,将条目汇集到一个可变的结果容器中,并可选择性地对结果执行最终转换。

1722
来自专栏高性能服务器开发

(三)dict哈希结构3

/* This function performs just a step of rehashing, and only if there are * no...

2678
来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

1573
来自专栏码匠的流水账

聊聊flink的SourceFunction

flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

1433
来自专栏闵开慧

javascript入门操作

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or...

37013

扫码关注云+社区