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...
}
java---- XMLEncoder 和 XMLDecoder 和 xSteam工具使用的更多相关文章
- 关于XMLEncoder和XMLDecoder
我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...
- Java数组操作利器:Arrays工具类
java.util.Arrays提供大量的工具方法来操作数组,这些方法全是静态方法. 1 便捷创建List public static <T> List<T> asList(T ...
- java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类、内部类应用于泛型探讨
java 泛型深入之Set有用工具 各种集合泛型深入使用演示样例,匿名内部类.内部类应用于泛型探讨 //Sets.java package org.rui.generics.set; import j ...
- Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm
JDK内置工具使用 一.javah命令(C Header and Stub File Generator) 二.jps命令(Java Virtual Machine Process Status To ...
- 【转】15款Java程序员必备的开发工具
如果你是一名Web开发人员,那么用膝盖想也知道你的职业生涯大部分将使用Java而度过.这是一款商业级的编程语言,我们没有办法不接触它. 对于Java,有两种截然不同的观点:一种认为Java是最简单功能 ...
- Java逆向武器库_反编译工具
1.反编译工具之_jd-gui 官网下载地址:http://java-decompiler.github.io/#jd-gui-download 使用: 下载后解压直接使用即可. jd-gui的优势是 ...
- 15款Java程序员必备的开发工具(转)
如果你是一名Web开发人员,那么用膝盖想也知道你的职业生涯大部分将使用Java而度过.这是一款商业级的编程语言,我们没有办法不接触它 对于Java,有两种截然不同的观点: 一种认为Java是最简单功能 ...
- Java判断不为空的工具类总结
1.Java判断是否为空的工具类,可以直接使用.包含,String字符串,数组,集合等等. package com.bie.util; import java.util.Collection; imp ...
- Java对象序列化和反序列化的工具方法
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
随机推荐
- Cnario 3.8支持哪些操作系统?
Cnario是基于Windows平台开发的软件,包含Server.Messenger和Player三个组件,支持以下Windows 版本系统: Server/Messenger 支持英文版的以下操作系 ...
- 蒟蒻浅谈树链剖分之一——两个dfs操作
树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...
- Spring MVC 使用介绍(九)—— 异常处理
一.概述 Spring MVC异常处理功能的作用为:捕捉处理器的异常,并映射到相应视图 有4种方式: SimpleMappingExceptionResolver:通过配置的方式实现异常处理,该方式简 ...
- python学习日记(OOP——@property)
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...
- 利用ESLINT进行js 语法检查,以及局部安装时遇到的一些坑
1)安装ESlint,这里我说一下全局安装,一会我会说我为什么不局部安装. npm install eslint -g 2)创建一个你的项目文件夹,随便起个名字吧,并初始化 cd myapp npm ...
- BZOJ4552 HEOI2016/TJOI2016排序(线段树合并+线段树分裂)
很久以前写过二分答案离线的做法,比较好理解.事实上这还是一个线段树合并+分裂的板子题,相比离线做法以更优的复杂度做了更多的事情.具体不说了.怎么交了一遍luogu上就跑第一了啊 #include< ...
- Linux基本命令总结(五)
接上篇: 21,在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是chang ...
- Vue(一)安装
环境准备 这里我们就直接使用官方推荐的Vue CLI方式 CLI (@vue/cli) 是一个全局安装的 npm 包,提供了终端里的 vue 命令.它可以通过 vue create 快速创建一个新项目 ...
- django系列8:优化vote页面,使用通用视图降低代码冗余
修改detail.html,将它变为一个可用的投票页面 <h1>{{ question.question_text }}</h1> {% if error_message %} ...
- 使用svn进行协作开发
环境 操作系统:win7 64位 所需工具 1. 服务器端(Subversion)[Setup-Subversion-1.8.16.msi] 2. 客户端(TortoiseSVN)[TortoiseS ...