XStream读取xml

在pom.xml中加入下面的依赖,就可以得到XStream相关的jar了

<dependency>

<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>

</dependency>

package com.mycomp.jaxb;


实体People类(对应people.xml文件读取完后注入到该java类):
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("people")
public class People {

@XStreamAlias("name")
private String name;

@XStreamAlias("age")
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString(){
return "name: " + this.name + ",age: " + this.age;
}
}

people.xml为与工程的resources文件夹下面:

<people>
<name>小王</name>
<age>21</age>
</people>

读取people.xml文件,并测试打印读取的结果:

package com.mycomp.jaxb; import java.io.InputStream; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class TestXstream { public static void main(String[] args) { XStream xStream = new XStream(new DomDriver()); InputStream inputStream = TestXstream.class.getClassLoader().getResourceAsStream("./people.xml"); xStream.processAnnotations(People.class); People people = (People) xStream.fromXML(inputStream); System.out.println(people); } }

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...