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包

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);
    }
}

  

利用xStream讀取XML和寫入XML

 

public class ProductClothesXML {
    public static List<Clothes> parseclothesXML() {
        List<Clothes> clothesList = new ArrayList<>();
        XStream xStream = new XStream(new Xpp3Driver());
        xStream.alias("list", clothesList.getClass());
        xStream.alias("cloth", Clothes.class);
        xStream.useAttributeFor(Clothes.class, "id");
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream("cloth.xml"));
            clothesList = (List<Clothes>) xStream.fromXML(bufferedInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return clothesList;
    }
    public static void writeClothesXML(List<Clothes> clothesList) {
        XStream xStream = new XStream(new Xpp3Driver());
        xStream.alias("list", clothesList.getClass());
        xStream.alias("cloth", Clothes.class);
        xStream.useAttributeFor(Clothes.class, "id");
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("cloth.xml"));
            bufferedOutputStream.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>".getBytes());
            xStream.toXML(clothesList, bufferedOutputStream);
            xStream.getClassLoader();
            bufferedOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

cloth.xml

<?xml version="1.0" encoding="UTF-8" ?>

<list>
    <cloth id="A01">
        <brand>adidas</brand>
        <color>白色</color>
        <style>襯衫</style>
        <size>s</size>
        <num>10</num>
        <price>100</price>
        <description>棉</description>
    </cloth>
    <cloth id="A02">
        <brand>adidas</brand>
        <color>黑色</color>
        <style>襯衫</style>
        <size>s</size>
        <num>20</num>
        <price>100</price>
        <description>棉</description>
    </cloth>
    <cloth id="A03">
        <brand>adidas</brand>
        <color>黑色</color>
        <style>球鞋</style>
        <size>M</size>
        <num>20</num>
        <price>500</price>
        <description>運動</description>
    </cloth>
</list>

Clothes類

public class Clothes implements Serializable {
    private String id;
    private String brand;
    private String style;
    private String color;
    private String size;
    private int num;
    private float price;
    private String description;
    set...
    get...      
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM