码迷,mamicode.com
首页 > 其他好文 > 详细

XStream 01

时间:2015-02-03 17:18:02      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:xml xstream

技术分享                                仅仅入门的第一步

                                
 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;


import com.thoughtworks.xstream.XStream;


public class Person {


private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
private static String classPath = Person.class.getResource("/").getPath().replaceAll("%20", " ");
 
public String toString() {
return this.firstname + "#" + this.lastname + "#" + this.phone + "#"
+ this.fax ;
}


public static void main(String[] args) throws FileNotFoundException {
 /***
  * 代码模式
  * 
  */

        XStream xstream = new XStream(new DomDriver());
        InputStream inputStream = new FileInputStream(new File(classPath.replace("classes", "/config.xml")));//xml存放得地址
        xstream.alias("person", Person.class);
        xstream.alias("phonennumber", PhoneNumber.class);
        Person joe = (Person)xstream.fromXML(inputStream);
        System.out.println(joe);
         
}


}






public class PhoneNumber {
private int code;
 private String number;
 
 public String toString() {
return this.code + "#" + this.number ;
}
 
}



技术分享

config.xml


<?xml version="1.0" encoding="UTF-8"?>
<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

XStream 01

标签:xml xstream

原文地址:http://blog.csdn.net/huuxuanhua_it/article/details/43451027

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!