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



發佈了36 篇原創文章 · 獲贊 34 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章