一个ip可以建设多少个网站,做网站还是app省钱,wordpress 列表样式,个人网站可以做百度推广简介
官网地址 http://x-stream.github.io 官网教程地址 http://x-stream.github.io/alias-tutorial.html XStream是一个简单的基于Java的类库#xff0c;用来将Java对象序列化成XML(JSON) 或 反序列化为对象 (即#xff1a;可以轻易的将Java对象和XML文档相互转换)。XStrea…简介
官网地址 http://x-stream.github.io 官网教程地址 http://x-stream.github.io/alias-tutorial.html XStream是一个简单的基于Java的类库用来将Java对象序列化成XML(JSON) 或 反序列化为对象 (即可以轻易的将Java对象和XML文档相互转换)。XStream在运行时使用Java反射机制对要进行序列化的对象树的结构进行探索并不需要对对象作出修改。
下载及依赖引入
官网下载 http://x-stream.github.io/download.html
上面下载地址网页中会有maven 引入版本
dependencygroupIdcom.thoughtworks.xstream/groupIdartifactIdxstream/artifactIdversion1.4.20/version
/dependency使用
1、创建 XStream 对象
1、XStream xStream new XStream();
【注意】默认为Xpp3解析器它是一个非常快速的 XML 拉取解析器实现。但是有依赖关系
需要引入 xstream-[version].jar 和 xpp3-[version].jar xmlpull-[version].jar 在类路径中
或
2、XStream xstream new XStream(new DomDriver());
【注意】使用标准的 JAXP DOM 解析器
或
3、XStream xstream new XStream(new StaxDriver());
【注意】从 Java 6 开始使用集成的 StAX 解析器StaxDriver使用SAX解析器(可从Java6)一个快速的XML解析器。2、java bean与xml 互转使用方法
序列化对象到 XML
// Object to XML Conversion
String xml xstream.toXML(Person);反序列化 XML 获得对象
// XML to Object Conversion
Person person (Person) xstream.fromXML(xml);
3、别名序列化映射关系使用
类别名
XStream.alias(String name, Class type)
xstream.alias(student, Student.class);如果不使用类别名标签会显示为完整名称
使用前com.org.Student 使用后student类属性别名
XStream.aliasField(String alias, Class definedIn, String fieldName)
xstream.aliasField(name, Student.class, studentName);使用前studentName使用后 name类属性混叠将类属性作为标签的属性而不是子标签
xstream.useAttributeFor(Student.class, studentName);使用前 studentstudentName
使用后 student studentNamexiaoxi隐式集合将使用的集合在 XML 无需显示集合节点直接显示集合的子节点数组或映射也可以声明为隐式
XStream.addImplicitCollection(Class ownerType, String fieldName)
xstream.addImplicitCollection(Student.class, noteList);使用前noteListnote/notenote/note/noteList显示了noteList节点
使用后note/notenote/note不显示noteList节点
用于解析没有根节点转换到list同时将list在转换到没有根节点的多组节点包别名
xstream.aliasPackage(com.Student, com.org.Student);使用后com.org.Student使用后com.Student转换器 属性不能直接转换的还可以使用转换器 参考转换器使用 http://x-stream.github.io/converter-tutorial.html
序列化与反序列化中映射关系使用方式
类别名方式 // 创建一个类的XML完全限定名称的别名xstream.alias(student, Student.class);xstream.alias(note, Note.class);// 使用的集合是表示在XML无需显示根xstream.addImplicitCollection(Student.class, notes);// 成员变量作为XML属性xstream.useAttributeFor(Student.class, studentName);// 用于创建以XML字段的别名xstream.aliasField(name, Student.class, studentName);注解别名方式
XStreamAlias(student) //类别名
class Student { XStreamAlias(name) //字段别名XStreamAsAttribute //同时设置字段为xml标签属性student namexiaoxi,没有XStreamAsAttribute就是xml的子标签private String studentName;XStreamImplicit //define list as an implicit collectionprivate ListNote notes new ArrayListNote();【关键】为了告诉 XStream 框架来处理注释需要XML序列化之前添加下面的命令。xstream.processAnnotations(Student.class);或者xstream.autodetectAnnotations(true);XStream xstream new XStream(new StaxDriver());
xstream.autodetectAnnotations(true);
反序列化 XML 获得对象
Person person (Person) xstream.fromXML(xml);
序列化对象到 XML
String xml xstream.toXML(Person);