极简设计

XStream

XStream

XStream 是一种序列化工具而不是数据绑定工具,就是说不能从 XML 或者 XML Schema Definition (XSD) 文件生成类 也支持json的转换(JSONDriver) 提供annotation注解

三特点: 1. 不关心序列化/逆序列化类字段可见性 2. 不需要 getter 和 setter 方法 3. 不需默认构造函数

package com.ddatsh;

public class Employee {
    private String name;
    private String designation;
    private String department;

    public String getName() {
        return name;
    }

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

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Name : " + this.name + "\nDesignation : " + this.designation
                + "\nDepartment : " + this.department;
    }
}
package com.ddatsh;

import java.io.FileOutputStream;

import com.thoughtworks.xstream.XStream;

public class Write {

    public static void main(String[] args) throws Exception {
        Employee e = new Employee();

        e.setName("Jack");
        e.setDesignation("Manager");
        e.setDepartment("Finance");

        XStream xs = new XStream();
        xs.alias("emp", Employee.class);
        //不加这句,root节点包含包名

        FileOutputStream fs = new FileOutputStream("z:/employeedata.txt");
        xs.toXML(e, fs);
        
        String xmlStr=xs.toXML(e);
        System.out.println(xmlStr);

    }
}
package com.ddatsh;

import java.io.FileInputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Reader {
    public static void main(String[] args) throws Exception {
        XStream xs = new XStream(new DomDriver());
        Employee e = new Employee();

        FileInputStream fis = new FileInputStream("z:/employeedata.txt");
        xs.fromXML(fis, e);

        System.out.println(e.toString());

    }
}

读配置


<?xml version="1.0" encoding="UTF-8"?>
<config>
 <datasource-name>IRIS</datasource-name>
 <ipaddress>9.124.74.85</ipaddress>
 <logfilename>DailyLogApplication.log</logfilename>
 <appender>console</appender>
</config>
package com.ddatsh;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ConfigReader {

    String datasourcename = null;
    String ipaddress = null;
    String logfilename = null;
    String appender = null;

    @Override
    public String toString() {
        return "Datasource Name : " + datasourcename + " \nIP Address : "
                + ipaddress + " \nLogfilename : " + logfilename
                + " \nAppender : " + appender;
    }


    public static void main(String[] args) throws FileNotFoundException {
        XStream xs = new XStream(new DomDriver());

        FileInputStream fis = new FileInputStream("z:/config.xml");
        xs.aliasField("datasource-name", ConfigReader.class, "datasourcename");
        xs.alias("config", ConfigReader.class);
        ConfigReader r = (ConfigReader) xs.fromXML(fis);

        System.out.println(r.toString());
    }
}

xs.aliasField("datasource-name", ConfigReader.class, "datasourcename"); 在不匹配时用 xs.alias("config", ConfigReader.class); 这个是root节点

别名

想要的效果


<blog author="DD"> 
  <entry> 
    <title>first</title> 
    <description>1</description> 
  </entry> 
  <entry> 
    <title>second</title> 
    <description>2</description> 
  </entry> 
</blog>
import java.util.ArrayList;
import java.util.List;

public class Blog {
    private Author author;
    private List entries = new ArrayList();

    public Blog(Author author) {
        this.author = author;
    }

    public void add(Entry entry) {
        entries.add(entry);
    }

    public List getContent() {
        return entries;
    }
}
public class Author {
    private String name;

    public Author(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
public class Entry {
    private String title, description;

    public Entry(String title, String description) {
        this.title = title;
        this.description = description;
    }
}
import com.thoughtworks.xstream.XStream;

public class XStreamTest {
    public static void main(String[] args) {
        Blog teamBlog = new Blog(new Author("dd"));
        teamBlog.add(new Entry("first", "1"));
        teamBlog.add(new Entry("second", "2"));
        XStream xstream = new XStream();
        System.out.println(xstream.toXML(teamBlog));
    }
}

<com.ddatsh.xstreamTest.Blog>
  <author>
    <name>dd</name>
  </author>
  <entries>
    <com.ddatsh.xstreamTest.Entry>
      <title>first</title>
      <description>1</description>
    </com.ddatsh.xstreamTest.Entry>
    <com.ddatsh.xstreamTest.Entry>
      <title>second</title>
      <description>2</description>
    </com.ddatsh.xstreamTest.Entry>
  </entries>
</com.ddatsh.xstreamTest.Blog>

去掉类全名

//两个类别名
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);

entries 去掉,变成多个 entrie

集合类型,不显示根标签(root tag),用implicit collection去映射 不希望出现entries标签,只需要一个接一个的列出所有的entry标签

xstream.addImplicitCollection(Blog.class, "entries");

name 放进author 属性里

把author成员变量设置为XML的属性 将author属性作为Blog类的"author"属性

xstream.useAttributeFor(Blog.class, "author");

Author转换成一个String对象让他在Blog节点中以author属性显示 SimpleValeConverter实现自己的Author转换器

import com.thoughtworks.xstream.converters.SingleValueConverter;

public class AuthorConverter implements SingleValueConverter {
    //用来转换什么类型的对象
    public boolean canConvert(Class type) {
        return type.equals(Author.class);
    }

    //Author实例转化成字符串
    public String toString(Object obj) {
        return ((Author) obj).getName();
    }

    //从字符串得到Author实例
    public Object fromString(String name) {
        return new Author(name);
    }
}

转化器注册到XStream

xstream.registerConverter(new AuthorConverter());

官方

http://xstream.codehaus.org/tutorial.html

XStream xstream = new XStream(); You require xstream-[version].jar and kxml2-min-[version].jar in the classpath kXML2 is a very fast XML pull-parser implementation If you do not want to include this dependency, you can use a standard JAXP DOM parser or since Java 6 the integrated StAX parser instead:

XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library

in java with : xstream, java