Xml和bean之间的相互转换

来源:互联网 发布:淘宝怎么投诉卖家手机 编辑:程序博客网 时间:2024/06/02 09:47

前言:

上一篇文章介绍了obj和xml之间的相互转换,其中xml以字符串为载体来呈现。下面介绍下复杂obj与xml文件之间的相互转换

一、准备测试的实体对象:

@XStreamAlias("person1")
public class Person{ 
@XStreamAsAttribute
private String version = "1.8";
@XStreamAsAttribute
private String encode = "UTF-8";
@XStreamAlias("firstName")
private String firstName;
@XStreamAlias("lastName")
private String lastName;
@XStreamAlias("telphone")
private PhoneNumber tel;
@XStreamAlias("faxOne")
private PhoneNumber fax;
@XStreamAlias("friends")
private Friend friend;
@XStreamAlias("pets")
private Pets pet;

}

其中:tel  fax   friend   pet  为复杂属性,

tel 和 fax :

@XStreamAlias("phoneNumber")
public class PhoneNumber {
@XStreamAlias("code")
private int code;
@XStreamAlias("number")
private String number;

}

friends:

public class Friend {
@XStreamImplicit(itemFieldName = "name")
private List<String> name;

}

pet

public class Pets {
@XStreamImplicit
private List<Animal> animalList;

}

@XStreamAlias("Animal")
public class Animal {
@XStreamAlias("name")
private String name;
@XStreamAlias("age")
private int age;

}

注解解释:

@XStreamAlias("")//别名注解,作用目标:类和字段

@XStreamAsAttribute //转换成属性,作用目标:字段

@XStreamImplicit(itemFieldName="part")  //作用目标:集合字段;

@XStreamImplicit //隐式集合

二、提供转换工具类与上一篇一样;

测试方法:

@Test
public void testToXml(){
Person per=new Person();
    per.setFirstName("chen");
    per.setLastName("youlong");
    PhoneNumber tel=new PhoneNumber();
    tel.setCode(137280);
    tel.setNumber("137280968");
    PhoneNumber fax=new PhoneNumber();
    fax.setCode(20);
    fax.setNumber("020221327");
    per.setTel(tel);
    per.setFax(fax);
    List<String> friendList=new ArrayList<String>();
    friendList.add("A1");
    friendList.add("A2");
    friendList.add("A3");
    Friend friend1=new Friend();
    friend1.setName(friendList);
    per.setFriend(friend1);
    Animal dog=new Animal("Dolly",2);
    Animal cat=new Animal("Ketty",2);
    List<Animal> petList=new ArrayList<Animal>();
    petList.add(dog);
    petList.add(cat);
    Pets pet=new Pets();
    pet.setAnimalList(petList);
    per.setPet(pet);
    String xml=XMLUtil.toXml(per);
    System.out.println(xml);
}

结果:

<person1 version="1.8" encode="UTF-8">
  <firstName>chen</firstName>
  <lastName>youlong</lastName>
  <telphone>
    <code>137280</code>
    <number>137280968</number>
  </telphone>
  <faxOne>
    <code>20</code>
    <number>020221327</number>
  </faxOne>
  <friends>
    <name>A1</name>
    <name>A2</name>
    <name>A3</name>
  </friends>
  <pets>
    <Animal>
      <name>Dolly</name>
      <age>2</age>
    </Animal>
    <Animal>
      <name>Ketty</name>
      <age>2</age>
    </Animal>
  </pets>
</person1>


下面介绍xml文件和obj相互转换的工具:

参数需要:obj对象,absPath输出路径,fileName文件名称

public static boolean toXMLFile(Object obj, String absPath, String fileName ){
String strXml = toXml(obj);
String filePath = absPath + fileName;//拼接路径
File file = new File(absPath);//上级目录路径
if(!file.exists())//检查问价夹是否存在
file.mkdirs();//不存在直接添加
file = new File(filePath);//获得输出流分file
if(!file.exists()){//检查
try {
file.createNewFile();//创建文件
}catch (IOException e) {
log.error("创建{"+ filePath +"}文件失败!!!" + e.getMessage());
return false ;
}
 
}
OutputStream ous = null ;
try {
ous = new FileOutputStream(file);//获得输出流
ous.write(strXml.getBytes());//进行写操作
ous.flush();
}catch (Exception e1) {
log.error("写{"+ filePath +"}文件失败!!!" + e1.getMessage());
return false;
}finally{
if(ous != null )
try {
ous.close();
} catch (IOException e) {
log.error("写{"+ filePath +"}文件关闭输出流异常!!!" + e.getMessage());
}
}
return true;
}

测试方法:

String url = "D:"+File.separator+"test"+File.separator;

boolean xmlFile = XMLUtil.toXMLFile(per, url, "test.xml");
System.out.println(xmlFile);


返回 true并且 得到D:\test\test.xml文件;




原创粉丝点击