您好,欢迎访问一九零五行业门户网

xml解析工具包 Xstream的示例代码详解

特点
简化的api; 
无映射文件; 
高性能,低内存占用; 
整洁的xml; 
不需要修改对象,支持内部私有字段;
不需要setter/getter方法,final字段;
提供序列化接口; 
自定义转换类型策略; 
详细的错误诊断; 
xstream常用注解
@xstreamalias(message) 别名注解,作用目标:类,字段
@xstreamimplicit 隐式集合  
@xstreamimplicit(itemfieldname=part)  作用目标:集合字段  
@xstreamconverter(singlevaluecalendarconverter.class) 注入转换器,作用目标: 对象  
@xstreamasattribute 转换成属性,作用目标:字段  
@xstreamomitfield 忽略字段,作用目标:字段  
示例
1. 解析xml工具类
import com.thoughtworks.xstream.xstream; import com.thoughtworks.xstream.io.xml.domdriver; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.io.*; /** * 输出xml和解析xml的工具类 */ public class xmlutil { private static final logger logger = loggerfactory. getlogger(xmlutil.class); /** * java 转换成xml * @param obj 对象实例 * @return string xml字符串 * @title: toxml * @description: todo */ public static string toxml(object obj) { //xstream xstream=new xstream(); //默认使用xpp解析器 //指定编码解析器 xstream xstream = new xstream(new domdriver("utf-8")); //启用注解识别 xstream.processannotations(obj.getclass()); return xstream.toxml(obj); } /** * 将传入xml文本转换成java对象 * @param xmlstr * @param cls xml对应的class类 * @return t xml对应的class类的实例对象 */ public static <t> t tobean(string xmlstr, class<t> cls) { xstream xstream = new xstream(); xstream.processannotations(cls); t obj = (t) xstream.fromxml(xmlstr); return obj; } /** * 写到xml文件中去 * @param obj 对象 * @param abspath 绝对路径 * @param filename 文件名 */ public static boolean toxmlfile(object obj, string abspath, string filename) { string strxml = toxml(obj); string filepath = abspath + filename; file file = new file(filepath); if (!file.exists()) { try { file.createnewfile(); } catch (ioexception e) { logger.error("file creation failed, cause is {}", e); return false; } } outputstream ous = null; try { ous = new fileoutputstream(file); ous.write(strxml.getbytes()); ous.flush(); } catch (exception e1) { logger.error("file write failed, cause is {}", e1); return false; } finally { if (ous != null) try { ous.close(); } catch (ioexception e) { e.printstacktrace(); } } return true; } /** * 从xml文件读取报文 * @param abspath 绝对路径 * @param filename 文件名 * @param cls */ public static <t> t tobeanfromfile(string abspath, string filename, class<t> cls) throws exception { string filepath = abspath + filename; inputstream ins = null; try { ins = new fileinputstream(new file(filepath)); } catch (exception e) { throw new exception("read {" + filepath + "} file failed!", e); } xstream xstream = new xstream(); xstream.processannotations(cls); t obj = null; try { obj = (t) xstream.fromxml(ins); } catch (exception e) { throw new exception("parse {" + filepath + "} file failed!", e); } if (ins != null) ins.close(); return obj; } }
2. 编写teacher类
import com.thoughtworks.xstream.annotations.xstreamalias; import com.thoughtworks.xstream.annotations.xstreamasattribute; import com.thoughtworks.xstream.annotations.xstreamimplicit; import java.util.list; @xstreamalias(value = "teacher") public class teacher { @xstreamasattribute private string name; @xstreamasattribute private string phone; @xstreamasattribute private int age; @xstreamimplicit(itemfieldname = "student") private list<student> students; public teacher() { } public teacher(string name, string phone, int age, list<student> students) { this.name = name; this.phone = phone; this.age = age; this.students = students; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public int getage() { return age; } public void setage(int age) { this.age = age; } public list<student> getstudents() { return students; } public void setstudents(list<student> students) { this.students = students; } }
3. 编写student类
import com.thoughtworks.xstream.annotations.xstreamalias; import com.thoughtworks.xstream.annotations.xstreamasattribute; @xstreamalias(value = "student") public class student { @xstreamasattribute private string name; @xstreamasattribute private int age; @xstreamasattribute private string address; public student() { } public student(string name, int age, string address) { this.name = name; this.age = age; this.address = address; } 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; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } }
4. test测试类
import java.util.arraylist; import java.util.list; public class test { public static void main(string[] args) { student student1 = new student("aaron", 24, "广州"); student student2 = new student("abel", 23, "北京"); list<student> students = new arraylist<>(); students.add(student1); students.add(student2); teacher teacher = new teacher("dave", "020-123456", 46, students); string xml = xmlutil.toxml(teacher); system.out.println(xml); } }
5. 运行结果
<teacher name="dave" phone="020-123456" age="46"> <student name="aaron" age="24" address="广州"/> <student name="abel" age="23" address="北京"/> </teacher>
以上就是xml解析工具包 xstream的示例代码详解的详细内容。
其它类似信息

推荐信息