它们都可以完成Java对象到XML的转换,但是还不是那么的完善。

还有XStream对JSON及XML的支持,它可以对JSON或XML的完美转换。在线博文:

Jibx对Java对象的转换相对要负责些,它不仅需要配置xml还且还要生成相应的jar文件,已经xsd文件。下面我们就来慢慢看看Jibx转换Java到XML是如何完成的。

一、准备工作

1、 准备资源

c) 依赖jar包如下:

2、 程序准备代码

package com.hoo.test;

import java.io.IOException;

import java.io.StringReader;

import java.io.StringWriter;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import org.jibx.runtime.BindingDirectory;

import org.jibx.runtime.IBindingFactory;

import org.jibx.runtime.IMarshallingContext;

import org.jibx.runtime.IUnmarshallingContext;

import org.jibx.runtime.JiBXException;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import com.hoo.entity.Account;

import com.hoo.entity.AccountArray;

import com.hoo.entity.Birthday;

import com.hoo.entity.ListBean;

import com.hoo.entity.MapBean;

/**

* function: Jibx转换Java到XML

* @author hoojo

* @createDate 2011-4-25 下午06:47:33

* @file JibxTest.java

* @package com.hoo.test

* @project WebHttpUtils

* @blog http://blog.csdn.net/IBM_hoojo

* @email hoojo_@126.com

* @version 1.0

*/

public class JibxTest {

private IBindingFactory factory = null;

private StringWriter writer = null;

private StringReader reader = null;

private Account bean = null;

@Before

public void init() {

bean = new Account();

bean.setAddress("北京");

bean.setEmail("email");

bean.setId(1);

bean.setName("jack");

Birthday day = new Birthday();

day.setBirthday("2010-11-22");

bean.setBirthday(day);

try {

factory = BindingDirectory.getFactory(Account.class);

} catch (JiBXException e) {

e.printStackTrace();

}

}

@After

public void destory() {

bean = null;

try {

if (writer != null) {

writer.flush();

writer.close();

}

if (reader != null) {

reader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

System.gc();

}

public void fail(Object o) {

System.out.println(o);

}

public void failRed(Object o) {

System.err.println(o);

}

}

IBindingFactory是一个工厂接口,通过BindingDirectory的getFactory工厂方法可以获得某个对象。然后通过这个工程可以获得转换xml文档的上下文。

二、转换Java到XML、转换XML到Java

1、 转换JavaEntity对象

a) 首先看看Account、Birthday的代码

package com.hoo.entity;

public class Account {

private int id;

private String name;

private String email;

private String address;

private Birthday birthday;

//getter、setter

@Override

public String toString() {

return this.id + "#" + this.name + "#" + this.email + "#" + this.address + "#" + this.birthday;

}

}

Birthday

package com.hoo.entity;

public class Birthday {

private String birthday;

public Birthday(String birthday) {

super();

this.birthday = birthday;

}

//getter、setter

public Birthday() {}

@Override

public String toString() {

return this.birthday;

}

}

b) 程序代码

@Test

public void bean2XML() {

try {

writer = new StringWriter();

// marshal 编组

IMarshallingContext mctx = factory.createMarshallingContext();

mctx.setIndent(2);

mctx.marshalDocument(bean, "UTF-8", null, writer);

fail(writer);

reader = new StringReader(writer.toString());

//unmarshal 解组

IUnmarshallingContext uctx = factory.createUnmarshallingContext();

Account acc = (Account) uctx.unmarshalDocument(reader, null);

fail(acc);

} catch (Exception e) {

e.printStackTrace();

}

}

这样还不够,复杂的东西还在后面。Jibx转换XML文档还要经过一系列复杂的程序。

c) 首先,要写bind.xml和schema。不过还好,官方有提高工具类可以用。

org.jibx.binding.generator.BindGen或org.jibx.binding.BindingGenerator这两个类都可以,用法如下:

首先用dos进入当前工程目录,然后执行命令:E:/Study/WebHttpUtils>java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.generator.BindGen -b bind.xml com.hoo.entity.Account

上面的java 是运行某个程序 –cp是依赖的classpath路径的jar、zip等文件,-b 是输出文件名称,是BindGen类的参数。这样会在当前工程目录中生成bind.xml和entity.xsd文件。先看看这2个文件

bind.xml

entity.xsd文件

elementFormDefault="qualified" targetNamespace="http://hoo.com/entity">

上面最重要的就是bind.xml文件了,下面编译的时候需要这个文件。Xsd文件可以根据这个文件的内容生成Java的Entity类代码。

执行完命令后,没有错误就可以运行下面一段命令了。运行命令:

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

-v是绑定文件的名称

运行后,有如下结果:

d) 然后你就可以运行上面的Java的Junit测试程序了,运行后结果如下:

jack

email

北京

2010-11-22

1#jack#email#北京#2010-11-22

你还可以用命令来查看某个已经生成bind、schema文件的信息,如:

java -cp bin;lib/jibx-run.jar org.jibx.runtime.PrintInfo -c com.hoo.entity.Account

结果如下:

e) 注意,有时候会出现异常信息,如:java.lang.NoSuchFieldException: JiBX_bindingXXXX就要重复下面的命令就可以了。

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

2、 转换带List集合属性的JavaBean

a) 程序代码

@Test

public void listBean2XML() {

try {

ListBean listBean = new ListBean();

Listlist = new ArrayList();

list.add(bean);

bean = new Account();

bean.setAddress("china");

bean.setEmail("tom@125.com");

bean.setId(2);

bean.setName("tom");

Birthday day = new Birthday("2010-11-22");

bean.setBirthday(day);

list.add(bean);

listBean.setList(list);

writer = new StringWriter();

factory = BindingDirectory.getFactory(ListBean.class);

// marshal 编组

IMarshallingContext mctx = factory.createMarshallingContext();

mctx.setIndent(2);

mctx.marshalDocument(listBean, "UTF-8", null, writer);

fail(writer);

reader = new StringReader(writer.toString());

//unmarshal 解组

IUnmarshallingContext uctx = factory.createUnmarshallingContext();

listBean = (ListBean) uctx.unmarshalDocument(reader, null);

fail(listBean.getList().get(0));

fail(listBean.getList().get(1));

} catch (Exception e) {

e.printStackTrace();

}

}

b) ListBean代码

package com.hoo.entity;

import java.util.List;

public class ListBean {

private String name;

private List list;

}

c) 生成bind.xml

执行dos命令:

java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.ListBean

输出:

d) 执行完后会生产bind.xml

Bind文件

e) 运行Compile工具类

在运行前,一定要将最先前运行的Account那个类的bind.xml文件的内容加入到现在这个bind.xml中,因为ListBean依赖了Account这个类。

命令如下:

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

运行后你可以看到最后出现这个

f) 运行Test程序,结果如下:

jack

email

北京

2010-11-22

tom

tom@125.com

china

2010-11-22

1#jack#email#北京#2010-11-22

2#tom#tom@125.com#china#2010-11-22

3、 转换Java对象数组

a) Test程序

/**

* function:转换对象数组

* @author hoojo

* @createDate 2011-4-26 下午05:32:03

*/

@Test

public void arrayBean2XML() {

try {

Account[] acc = new Account[2];

acc[0] = bean;

bean = new Account();

bean.setName("tom");

bean.setId(223);

acc[1] = bean;

AccountArray array = new AccountArray();

array.setAccounts(acc);

writer = new StringWriter();

factory = BindingDirectory.getFactory(AccountArray.class);

// marshal 编组

IMarshallingContext mctx = factory.createMarshallingContext();

mctx.setIndent(2);

mctx.marshalDocument(array, "UTF-8", null, writer);

fail(writer);

reader = new StringReader(writer.toString());

//unmarshal 解组

IUnmarshallingContext uctx = factory.createUnmarshallingContext();

array = (AccountArray) uctx.unmarshalDocument(reader, null);

fail(array.getAccounts()[0]);

fail(array.getAccounts()[1]);

} catch (Exception e) {

e.printStackTrace();

}

}

b) AccountArray代码

package com.hoo.entity;

public class AccountArray {

private Account[] accounts;

private int size;

public int getSize() {

size = accounts.length;

return size;

}

public void setSize(int size) {

this.size = size;

}

public Account[] getAccounts() {

return accounts;

}

public void setAccounts(Account[] accounts) {

this.accounts = accounts;

}

}

c) 运行命令生成bind.xml文件

命令如下:

java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.Account com.hoo.entity.AccountArray

因为AccountArray依赖Account,所以后面带2个类

d) 运行Compile命令

java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

e) 执行完后,就可以运行Test程序了,结果如下

jack

email

北京

2010-11-22

tom

1#jack#email#北京#2010-11-22

223#tom#null#null#null

4、 转换带Map结合的JavaEntity对象

a) Test代码

/**

* function:转换Map集合

* @author hoojo

* @createDate 2011-4-26 下午05:40:34

*/

@Test

public void mapBean2XML() {

try {

MapBean mapBean = new MapBean();

HashMapmap = new HashMap();

map.put("No1", bean);

bean = new Account();

bean.setAddress("china");

bean.setEmail("tom@125.com");

bean.setId(2);

bean.setName("tom");

Birthday day = new Birthday("2010-11-22");

bean.setBirthday(day);

map.put("No2", bean);

mapBean.setMap(map);

factory = BindingDirectory.getFactory(MapBean.class);

writer = new StringWriter();

// marshal 编组

IMarshallingContext mctx = factory.createMarshallingContext();

mctx.setIndent(2);

mctx.marshalDocument(mapBean, "UTF-8", null, writer);

fail(writer);

reader = new StringReader(writer.toString());

//unmarshal 解组

IUnmarshallingContext uctx = factory.createUnmarshallingContext();

mapBean = (MapBean) uctx.unmarshalDocument(reader, null);

fail(mapBean.getMap());

fail(mapBean.getMap().get("No1"));

fail(mapBean.getMap().get("No2"));

} catch (Exception e) {

e.printStackTrace();

}

}

b) MapBean代码

package com.hoo.entity;

import java.util.HashMap;

public class MapBean {

private HashMapmap;

public HashMapgetMap() {

return map;

}

public void setMap(HashMapmap) {

this.map = map;

}

}

c) 生成bind.xml,命令如下

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-tools.jar;lib/log4j-1.2.16.jar org.jibx.binding.BindingGenerator -f bind.xml com.hoo.entity.Account com.hoo.entity.MapBean

运行后,会生产bind.xml;修改bind.xml内容如下:

marshaller="com.hoo.util.HashMapper" unmarshaller="com.hoo.util.HashMapper">

注意上面的MapBean的structure元素的内容是经过修改的。一定要带上marshaller或unmarshaller,不然无法转换HashMap的。

d) HashMapper代码

package com.hoo.util;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import org.jibx.runtime.IAliasable;

import org.jibx.runtime.IMarshallable;

import org.jibx.runtime.IMarshaller;

import org.jibx.runtime.IMarshallingContext;

import org.jibx.runtime.IUnmarshaller;

import org.jibx.runtime.IUnmarshallingContext;

import org.jibx.runtime.JiBXException;

import org.jibx.runtime.impl.MarshallingContext;

import org.jibx.runtime.impl.UnmarshallingContext;

/**

* function:http://www.java2s.com/Open-Source/Java/XML/JiBX/tutorial/example21/HashMapper.java.htm

* @file HashMapper.java

* @package com.hoo.util

* @project WebHttpUtils

* @blog http://blog.csdn.net/IBM_hoojo

* @email hoojo_@126.com

* @version 1.0

*/

public class HashMapper implements IMarshaller, IUnmarshaller, IAliasable

{

private static final String SIZE_ATTRIBUTE_NAME = "size";

private static final String ENTRY_ELEMENT_NAME = "entry";

private static final String KEY_ATTRIBUTE_NAME = "key";

private static final int DEFAULT_SIZE = 10;

private String m_uri;

private int m_index;

private String m_name;

public HashMapper() {

m_uri = null;

m_index = 0;

m_name = "hashmap";

}

public HashMapper(String uri, int index, String name) {

m_uri = uri;

m_index = index;

m_name = name;

}

/* (non-Javadoc)

* @see org.jibx.runtime.IMarshaller#isExtension(int)

*/

public boolean isExtension(int index) {

return false;

}

/* (non-Javadoc)

* @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object,

* org.jibx.runtime.IMarshallingContext)

*/

public void marshal(Object obj, IMarshallingContext ictx)

throws JiBXException {

// make sure the parameters are as expected

if (!(obj instanceof HashMap)) {

throw new JiBXException("Invalid object type for marshaller");

} else if (!(ictx instanceof MarshallingContext)) {

throw new JiBXException("Invalid object type for marshaller");

} else {

// start by generating start tag for container

MarshallingContext ctx = (MarshallingContext)ictx;

HashMap map = (HashMap)obj;

ctx.startTagAttributes(m_index, m_name).

attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()).

closeStartContent();

// loop through all entries in hashmap

Iterator iter = map.entrySet().iterator();

while (iter.hasNext()) {

Map.Entry entry = (Map.Entry)iter.next();

ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME);

if (entry.getKey() != null) {

ctx.attribute(m_index, KEY_ATTRIBUTE_NAME,

entry.getKey().toString());

}

ctx.closeStartContent();

if (entry.getValue() instanceof IMarshallable) {

((IMarshallable)entry.getValue()).marshal(ctx);

ctx.endTag(m_index, ENTRY_ELEMENT_NAME);

} else {

throw new JiBXException("Mapped value is not marshallable");

}

}

// finish with end tag for container element

ctx.endTag(m_index, m_name);

}

}

/* (non-Javadoc)

* @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)

*/

public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException {

return ctx.isAt(m_uri, m_name);

}

/* (non-Javadoc)

* @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object,

* org.jibx.runtime.IUnmarshallingContext)

*/

public Object unmarshal(Object obj, IUnmarshallingContext ictx)

throws JiBXException {

// make sure we're at the appropriate start tag

UnmarshallingContext ctx = (UnmarshallingContext)ictx;

if (!ctx.isAt(m_uri, m_name)) {

ctx.throwStartTagNameError(m_uri, m_name);

}

// create new hashmap if needed

int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE);

HashMap map = (HashMap)obj;

if (map == null) {

map = new HashMap(size);

}

// process all entries present in document

ctx.parsePastStartTag(m_uri, m_name);

while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) {

Object key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME, null);

ctx.parsePastStartTag(m_uri, ENTRY_ELEMENT_NAME);

Object value = ctx.unmarshalElement();

map.put(key, value);

ctx.parsePastEndTag(m_uri, ENTRY_ELEMENT_NAME);

}

ctx.parsePastEndTag(m_uri, m_name);

return map;

}

public boolean isExtension(String arg0) {

return false;

}

}

e) 然后运行Compile命令

E:/Study/WebHttpUtils>java -cp bin;lib/jibx-bind.jar org.jibx.binding.Compile -v bind.xml

f) 结果如下

tom

tom@125.com

china

2010-11-22

jack

email

北京

2010-11-22

{No2=2#tom#tom@125.com#china#2010-11-22, No1=1#jack#email#北京#2010-11-22}

1#jack#email#北京#2010-11-22

2#tom#tom@125.com#china#2010-11-22

java jibx_Jibx 处理XML相关推荐

  1. Referenced file contains errors (http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_4.xsd).

    项目导入后,一个tld文件提示以下错误: Referenced file contains errors (http://java.sun.com/xml/ns/j2ee/web-jsptaglibr ...

  2. Java对象转xml报文和xml报文转Java对象帮助类

    import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marsha ...

  3. Java 中的 XML:Java 文档模型的用法

    Java 中的 XML:Java 文档模型的用法 英文原文 内容: 代码对比 DOM JDOM dom4j Electric XML XPP 结束语 下一次... 参考资料 关于作者 对本文的评价 相 ...

  4. 如何从Java中打印XML?

    本文翻译自:How to pretty print XML from Java? I have a Java String that contains XML, with no line feeds ...

  5. java bean与xml转换_Java Bean与xml互相转换的方法分析

    本文实例讲述了Java Bean与xml互相转换的方法.分享给大家供大家参考,具体如下: XML和Java Bean互相转换是一个很有用的功能,因为两者有着前后合作的关系,但解析的过程比较痛苦.下面介 ...

  6. JAVA Bean和XML之间的相互转换 - XStream简单入门

    JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...

  7. java 转xml 变成两根下划线_XStream将java对象转换为xml时,对象字段中的下划线“_”,转换后变成了两个...

    使用XStream将java对象转换为xml时,需要对其中的一个字段加上CDATA,于是我自定义了一个XStream,主要代码如下:publicstaticXStreamcreateXstream() ...

  8. xsd java引用_web.xml文件的 xsd引用(或dtd引用)学习

    1. 为什么web.xml会有不同版本的xsd引用: JDK依赖变化: 或 servlet(JAVA EE)自身API的改变: 2. 为什么会有dtd和xsd两个版本的区别 我是在这篇文章中看到的,作 ...

  9. Mabatis 源码探究(2)Java 获取mybatis-config.xml的输入流 inputStream对象

    关于Mybatis源码探究的专栏. 其我的专业是软件技术这个方向的,mybatis 许久以前就学了,但是心里一直存在一些疑惑.也上网查了,看过各种大佬的博客,对 于Mybatis的理解始终感觉不足.最 ...

最新文章

  1. python - 装饰器(重点,难点(比较绕))
  2. delphi tabsheet多标签自适应宽度_HTML 图像 img 标签
  3. LPS25HB 寄存器读写程序解读
  4. Mac盖上屏幕后外接屏幕持续黑画面的解决方法
  5. 计算机组装实验硬盘分区方法,硬盘怎么分区和格式化 史上最详细的硬盘分区方法大全 (全文)...
  6. 全向移动小车运动控制_如何让机器人进行全向移动
  7. 链表-链表中环的入口结点
  8. ubuntu 安装 hustoj
  9. 【Bzoj2242】计算器
  10. GreatSQL配置到系统systemd服务
  11. 基于freeswitch1.6的IVR智能语音机器人交互逻辑lua脚本
  12. 美颜sdk常用功能的实现原理
  13. 我的编程之路(三) 蜗居
  14. 运算器和控制器的组成部件及功能
  15. 百度地图:创建多个标注点
  16. 卡尔曼滤波 - 状态空间模型中的状态方程
  17. 火车头采集翻页内容_八爪鱼采集器 循环翻页只翻3、5页就提示采集结束
  18. 独家记忆孙嘉灵海棠首发 婉转乐曲演绎动心爱情
  19. afudos备份bios不动_怎么使用AFUDOS备份和升级BIOS?
  20. java的字节码文件是什么后缀_【Java虚拟机1】Java字节码文件格式入门

热门文章

  1. 如何区分网线是几类的_认清网线的六种材质,挑选网线不再被坑
  2. 学习3D视觉,圈子很重要!!!
  3. 开挂的 00 后!17 岁「天才少女」被 8 所世界名校录取,最终选择 MIT 计算机系...
  4. SLAM/检测跟踪/多传感器融合方向实习生招聘 | 腾讯Robotics X实验室
  5. 一文详解手眼标定之九点法
  6. 一分钟详解「本质矩阵」推导过程
  7. windows 如何cmd启动redis
  8. 离散数学当中的部分符号总结
  9. 命名管道 win7未响应_大数据分析Python建立分析数据管道
  10. windows如何添加本机dns记录_如何规避Sysmon DNS监控