android xstream 解析xml
2014-08-05 16:56
435 查看
第三方包:xstream-1.3.1.jar
1.序列号对象为xml
<span style="font-family:Arial;background-color: rgb(255, 255, 255);"> </span><pre class="html" name="code">XStream xStream = new XStream();
xStream.alias("person", Person.class);
xStream.alias("personnumber", PhoneNumber.class);
// Serializing an object to XML
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
FileOutputStream fileOutputStream;
String path = "/mnt/sdcard/test.xml";
File f = new File(path);
if (!f.exists())
f.createNewFile();
fileOutputStream = new FileOutputStream(f);
xStream.toXML(joe, fileOutputStream);
// Deserializing an object back from XM
// XStream xStream2 = new XStream();
fileOutputStream.close();
2.反序列号xml为对象。注意用domdriver,并且alias、userAttributeFor,必须定义。不然报错。
XStream xStream2 = new XStream(new DomDriver());
xStream2.alias("person", Person.class);
xStream2.alias("personnumber", PhoneNumber.class);
xStream2.useAttributeFor(Person.class, "firstname");
xStream2.useAttributeFor(Person.class, "lastname");
xStream2.useAttributeFor(PhoneNumber.class, "code");
xStream2.useAttributeFor(PhoneNumber.class, "number");
FileInputStream fileInputStream = new FileInputStream(f);
Person person = (Person) xStream2.fromXML(fileInputStream);
String str = person.toString();
EditText t = (EditText) findViewById(R.id.editText1);
t.setText(str);
fileInputStream.close();
1.序列号对象为xml
<span style="font-family:Arial;background-color: rgb(255, 255, 255);"> </span><pre class="html" name="code">XStream xStream = new XStream();
xStream.alias("person", Person.class);
xStream.alias("personnumber", PhoneNumber.class);
// Serializing an object to XML
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
FileOutputStream fileOutputStream;
String path = "/mnt/sdcard/test.xml";
File f = new File(path);
if (!f.exists())
f.createNewFile();
fileOutputStream = new FileOutputStream(f);
xStream.toXML(joe, fileOutputStream);
// Deserializing an object back from XM
// XStream xStream2 = new XStream();
fileOutputStream.close();
2.反序列号xml为对象。注意用domdriver,并且alias、userAttributeFor,必须定义。不然报错。
XStream xStream2 = new XStream(new DomDriver());
xStream2.alias("person", Person.class);
xStream2.alias("personnumber", PhoneNumber.class);
xStream2.useAttributeFor(Person.class, "firstname");
xStream2.useAttributeFor(Person.class, "lastname");
xStream2.useAttributeFor(PhoneNumber.class, "code");
xStream2.useAttributeFor(PhoneNumber.class, "number");
FileInputStream fileInputStream = new FileInputStream(f);
Person person = (Person) xStream2.fromXML(fileInputStream);
String str = person.toString();
EditText t = (EditText) findViewById(R.id.editText1);
t.setText(str);
fileInputStream.close();
相关文章推荐
- android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件
- android XMl 解析神奇xstream 六: 把集合list 转化为 XML文档
- android XMl 解析神奇xstream 二: 把对象转换成xml
- android 使用XStream解析xml
- android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象
- android XMl 解析神奇xstream 六: 把集合list 转化为 XML文档
- android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件
- android下通过xstream解析XML
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
- android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
- android 使用XStream解析xml的实例
- android XMl 解析神奇xstream 二: 把对象转换成xml
- Android 用 XStream 将复杂xml解析为javabean
- android XMl 解析神奇xstream 三: 把复杂对象转换成 xml
- android中解析复杂xml(XStream简单使用)
- Android中使用XStream解析XML
- Android 在引用Xstream 解析xml 编译报错问题解决办法
- Android高手进阶教程(二十一)之---Android中创建与几种解析xml的方法!
- bak 6.android xml解析 XmlPullParser的使用.