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...
}

  

java---- XMLEncoder 和 XMLDecoder 和 xSteam工具使用的更多相关文章

  1. 关于XMLEncoder和XMLDecoder

    我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...

  2. Java数组操作利器:Arrays工具类

    java.util.Arrays提供大量的工具方法来操作数组,这些方法全是静态方法. 1 便捷创建List public static <T> List<T> asList(T ...

  3. java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类、内部类应用于泛型探讨

    java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类.内部类应用于泛型探讨 //Sets.java package org.rui.generics.set; import j ...

  4. Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm

    JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...

  5. 【转】15款Java程序员必备的开发工具

    如果你是一名Web开发人员,那么用膝盖想也知道你的职业生涯大部分将使用Java而度过.这是一款商业级的编程语言,我们没有办法不接触它. 对于Java,有两种截然不同的观点:一种认为Java是最简单功能 ...

  6. Java逆向武器库_反编译工具

    1.反编译工具之_jd-gui 官网下载地址:http://java-decompiler.github.io/#jd-gui-download 使用: 下载后解压直接使用即可. jd-gui的优势是 ...

  7. 15款Java程序员必备的开发工具(转)

    如果你是一名Web开发人员,那么用膝盖想也知道你的职业生涯大部分将使用Java而度过.这是一款商业级的编程语言,我们没有办法不接触它 对于Java,有两种截然不同的观点: 一种认为Java是最简单功能 ...

  8. Java判断不为空的工具类总结

    1.Java判断是否为空的工具类,可以直接使用.包含,String字符串,数组,集合等等. package com.bie.util; import java.util.Collection; imp ...

  9. Java对象序列化和反序列化的工具方法

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

随机推荐

  1. sql sever基本命令

    创建表: create table stu_info( id ,) not null primary key clustered, name ) not null, score numeric not ...

  2. PLSQL:orecal,tnsname简介

    导入ORACLE遇到很多问题,学了好多,其中很长时间花在网络配置上,刚开始学,具体原因不知道,先把搜集到的好文章存下来,以后慢慢研究. 监听配置文件             为了使得外部进程 如 CA ...

  3. LIS的O(nlogn)算法

    出自蓝书<算法竞赛入门经典训练指南> 求最长上升子序列是很常见的可以用动态规划解决的问题…… 很容易根据最优子结构之类的东西得出 $\text{dp}[i]$为以第i个数结尾的最长上升子序 ...

  4. D. Maximum Diameter Graph 贪心+图论+模拟

    题意:给出n个点的度数列 上限(实际点可以小于该度数列)问可以构造简单路最大长度是多少(n个点要连通 不能有平行边.重边) 思路:直接构造一条长链  先把度数为1的点 和度数大于1的点分开  先把度数 ...

  5. TsinsenA1221 大楼【矩阵快速幂】

    题目分析: 重新定义矩阵运算,$*$等价于$+$,$+$等价于$max$. 然后倍增一下,再二分一下. 代码: #include<bits/stdc++.h> using namespac ...

  6. 走进Java中的持有对象(容器类)之一 容器分类

    Java容器可以说是增强程序员编程能力的基本工具,本系列将带您深入理解容器类. 容器的用途 如果对象的数量与生命周期都是固定的,自然我们也就不需要很复杂的数据结构. 我们可以通过创建引用来持有对象,如 ...

  7. User-Agent 请求消息头

    User-Agent User-Agent, 用户代理  请求消息头,其中包含了 客户机.客户端 的一些信息, 如 浏览器版本 和 类型,  操作系统的类型等. 具体解析 步骤, 推荐以下 博客文章 ...

  8. maven wrapper使用本地maven

    修改maven-wrapper.properties内容如下: #distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apa ...

  9. BFC块级格式化上下文

    BFC块级格式化上下文 触发条件 overflow 值不为 visible 的块元素 根元素 html 元素 浮动元素(元素的 float 不是 none) 绝对定位元素(元素的 position 为 ...

  10. IDEA打印gc日志,设置JVM参数方法

    打印gc日志 1.对指定运行程序输出GC日志: 点击edit configurations... 在vm options处加入-XX:+PrintGCDetails 测试:代码调用system.gc后 ...