Java对象序列化与反序列化二 XML

本文介绍使用XStream库进行Java对象到XML的序列化与反序列化过程,包括基本对象、数组、集合、Map及枚举类型的处理,并演示了特殊字符的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 依赖库

 

xstream-1.3.1.jar

xpp3_min-1.1.4c.jar

 

2. 代码

 

public class Student {
    private String name;
    private int age;
   
    public Student(){
       
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    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 class Grade {
   
    public String name = "wang";
    public int f1 = 100;

}

public enum Color {
    RED, GREEN, BLUE
}



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;

public class XmlTest {

    public static void main(String[] args) {
        XStream xstream = new XStream();

        String serialValue = xstream.toXML(new Student("wang", 20));
        System.out.println(serialValue);
        Student student = (Student)xstream.fromXML(serialValue);
        System.out.println(student.getName());
       
        //数组
        String[] arr1 = {"111", "222"};
        serialValue = xstream.toXML(arr1);
        System.out.println(serialValue);
        String[] arr2 = (String[])xstream.fromXML(serialValue);
        System.out.println(arr2[0]);
       
        //集合
        List<Student> list1 = new ArrayList<Student>();
        list1.add(new Student("wang", 20));
        list1.add(new Student("zhang", 21));
        serialValue = xstream.toXML(list1);
        System.out.println(serialValue);
        List<Student> list2 = (List<Student>)xstream.fromXML(serialValue);
        System.out.println(list2.get(0).getName());
       
        //MAP
        Map<Integer, String> map1 = new HashMap<Integer, String>();
        map1.put(1, "11");
        map1.put(2, "22");
        serialValue = xstream.toXML(map1);
        System.out.println(serialValue);
        Map<Integer, String> map2 = (Map<Integer, String>)xstream.fromXML(serialValue);
        System.out.println(map2.get(1));
       
        //枚举
        Color color = Color.RED;
        serialValue = xstream.toXML(color);
        System.out.println(serialValue);
        Color color2 = (Color)xstream.fromXML(serialValue);
        System.out.println(color2);
       
        //注入测试
        serialValue = xstream.toXML(new Student("</name><wang>yuan</wang>", 20));
        System.out.println(serialValue);
        Student student3 = (Student)xstream.fromXML(serialValue);
        System.out.println(student3.getName());
       
        serialValue = xstream.toXML(new Grade());
        System.out.println(serialValue);
        Grade g = (Grade)xstream.fromXML(serialValue);
        System.out.println(g.name);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值