java---- XMLEncoder 和 XMLDecoder 和 xSteam工具使用

 XMLEncoder:

将对象写入XML数据中

import org.dom4j.DocumentException;
import java.beans.XMLEncoder;
import java.io.*;
public class Demo{
    public static void main(String[] args) throws IOException, DocumentException {
        xmlEncoder();
    }
    //将对象写入XML文档中
    private static void xmlEncoder() throws DocumentException, FileNotFoundException {
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("test.xml"));
        XMLEncoder xmlEncoder = new XMLEncoder(bufferedOutputStream);
        //实例化的类必须是public 否则会报错
        Person person = new Person();
        person.setAge("10");
        person.setName("bin");
        person.setId("p0");
        xmlEncoder.writeObject(person);
        xmlEncoder.close();
    }
}

对象必须有public

public class Person {
        private String name;
        private String age;
        private String id;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAge() {
            return age;
        }
        public void setAge(String age) {
            this.age = age;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age='" + age + '\'' +
                    ", id='" + id + '\'' +
                    '}';
        }
}

  

 XMLDecoder:

import org.dom4j.DocumentException;
import java.beans.XMLDecoder;
import java.io.*;

public class Demo {
    public static void main(String[] args) throws IOException, DocumentException {
        xmlEncoder();
    }

    //将对象从XML文档中读出来
    private static void xmlEncoder() throws DocumentException, FileNotFoundException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("test.xml"));
        XMLDecoder xmlDncoder = new XMLDecoder(bufferedInputStream);
        Person perosn = (Person) xmlDncoder.readObject();
        System.out.println(perosn.getAge());
        System.out.println(perosn.getName());
        System.out.println(perosn.getId());
        xmlDncoder.close();
    }
}

  

 xStream工具使用:

适合用作数据传输

下载xSream jar包

扫描二维码关注公众号,回复: 6021583 查看本文章
http://x-stream.github.io/download.html

下载依赖包 xpp3  jar包

http://www.extreme.indiana.edu/dist/java-repository/xpp3/distributions/

  

使用实例

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


public class Demo {
    public static void main(String[] args){
        //适合用作数据传输
        xStream();
    }
    private static void xStream(){
        //生成xml数据
        XStream xStream = new XStream(new Xpp3Driver());
        //设置安全,不然会出现警告 Security framework of XStream not initialized, XStream is probably vulnerable
        XStream.setupDefaultSecurity(xStream);
        xStream.allowTypes(new Class[]{Person.class, Person.class});

        Person person = new Person();
        person.setId("p1");
        person.setName("花花");
        person.setAge("10");
        //取别名
        xStream.alias("person",Person.class);
        //别名的属性用id来设置
        xStream.useAttributeFor(Person.class,"id");
        String xml = xStream.toXML(person);
        System.out.println(xml);

        //解析xml数据
        Person person1 = (Person) xStream.fromXML(xml);
        System.out.println(person1);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/yanxiaoge/p/10772461.html