您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Jibx-处理XML
一、准备工作1、准备资源a)官方示例:)Jar下载:)依赖jar包如下:2、程序准备代码packagecom.hoo.test;importjava.io.IOException;importjava.io.StringReader;importjava.io.StringWriter;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importorg.jibx.runtime.BindingDirectory;importorg.jibx.runtime.IBindingFactory;importorg.jibx.runtime.IMarshallingContext;importorg.jibx.runtime.IUnmarshallingContext;importorg.jibx.runtime.JiBXException;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importcom.hoo.entity.Account;importcom.hoo.entity.AccountArray;importcom.hoo.entity.Birthday;importcom.hoo.entity.ListBean;importcom.hoo.entity.MapBean;/***bfunction:/bJibx转换Java到XML*@authorhoojo*@createDate2011-4-25下午06:47:33*@fileJibxTest.java*@packagecom.hoo.test*@projectWebHttpUtils*@blog*@emailhoojo_@126.com*@version1.0*/publicclassJibxTest{privateIBindingFactoryfactory=null;privateStringWriterwriter=null;privateStringReaderreader=null;privateAccountbean=null;@Beforepublicvoidinit(){bean=newAccount();bean.setAddress(北京);bean.setEmail(email);bean.setId(1);bean.setName(jack);Birthdayday=newBirthday();day.setBirthday(2010-11-22);bean.setBirthday(day);try{factory=BindingDirectory.getFactory(Account.class);}catch(JiBXExceptione){e.printStackTrace();}}@Afterpublicvoiddestory(){bean=null;try{if(writer!=null){writer.flush();writer.close();}if(reader!=null){reader.close();}}catch(IOExceptione){e.printStackTrace();}System.gc();}publicvoidfail(Objecto){System.out.println(o);}publicvoidfailRed(Objecto){System.err.println(o);}}IBindingFactory是一个工厂接口,通过BindingDirectory的getFactory工厂方法可以获得某个对象。然后通过这个工程可以获得转换xml文档的上下文。二、转换Java到XML、转换XML到Java1、转换JavaEntity对象a)首先看看Account、Birthday的代码packagecom.hoo.entity;publicclassAccount{privateintid;privateStringname;privateStringemail;privateStringaddress;privateBirthdaybirthday;//getter、setter@OverridepublicStringtoString(){returnthis.id+#+this.name+#+this.email+#+this.address+#+this.birthday;}}Birthdaypackagecom.hoo.entity;publicclassBirthday{privateStringbirthday;publicBirthday(Stringbirthday){super();this.birthday=birthday;}//getter、setterpublicBirthday(){}@OverridepublicStringtoString(){returnthis.birthday;}}b)程序代码@Testpublicvoidbean2XML(){try{writer=newStringWriter();//marshal编组IMarshallingContextmctx=factory.createMarshallingContext();mctx.setIndent(2);mctx.marshalDocument(bean,UTF-8,null,writer);fail(writer);reader=newStringReader(writer.toString());//unmarshal解组IUnmarshallingContextuctx=factory.createUnmarshallingContext();Accountacc=(Account)uctx.unmarshalDocument(reader,null);fail(acc);}catch(Exceptione){e.printStackTrace();}}这样还不够,复杂的东西还在后面。Jibx转换XML文档还要经过一系列复杂的程序。c)首先,要写bind.xml和schema。不过还好,官方有提高工具类可以用。org.jibx.binding.generator.BindGen或org.jibx.binding.BindingGenerator这两个类都可以,用法如下:首先用dos进入当前工程目录,然后执行命令:E:\Study\WebHttpUtilsjava-cpbin;lib/jibx-tools.jar;lib/log4j-1.2.16.jarorg.jibx.binding.generator.BindGen-bbind.xmlcom.hoo.entity.Account上面的java是运行某个程序–cp是依赖的classpath路径的jar、zip等文件,-b是输出文件名称,是BindGen类的参数。这样会在当前工程目录中生成bind.xml和entity.xsd文件。先看看这2个文件bind.xml?xmlversion=1.0encoding=UTF-8?bindingvalue-style=attributemappingclass=com.hoo.entity.Accountname=accountvaluename=idfield=id/valuestyle=elementname=namefield=nameusage=optional/valuestyle=elementname=emailfield=emailusage=optional/valuestyle=elementname=addressfield=addressusage=optional/structurefield=birthdayusage=optionalname=birthdayvaluestyle=elementname=birthdayfield=birthdayusage=optional//structure/mapping/bindingentity.xsd文件xs:schemaxmlns:xs=:tns==qualifiedtargetNamespace=:elementtype=tns:accountname=account/xs:complexTypename=accountxs:sequencexs:elementtype=xs:stringname=nameminOccurs=0/xs:elementtype=xs:stringname=emailminOccurs=0/xs:elementtype=xs:stringname=addressminOccurs=0/xs:elementname=birthdayminOccurs=0xs:complexTypexs:sequencexs:elementtype=xs:stringname=birthdayminOccurs=0//xs:sequence/xs:complexType/xs:element/xs:sequencexs:attributetype=xs:intuse=requiredname=id//xs:complexType/xs:schema上面最重要的就是bind.xml文件了,下面编译的时候需要这个文件。Xsd文件可以根据这个文件的内容生成Java的Entity类代码。执行完命令后,没有错误就可以运行下面一段命令了。运行命令:E:\Study\WebHttpUtilsjava-cpbin;lib/jibx-bind.jarorg.jibx.binding.Compile-vbind.xml-v是绑定文件的名称运行后,有如下结果:d)然后你就可以运行上面的Java的Junit测试程序了,运行后结果如下:?xmlversion=1.0encoding=UTF-8?accountxmlns==1namejack/nameemailemail/emailaddress北京/addressbirthdaybirthday2010-11-22/birthday/birthday/account1#jack#email#北京#2010-11-22你还可以用命令来查看某个已经生成bind、schema文件的信息,如:java-cpbin;lib/jibx-run.jarorg.jibx.runtime.PrintInfo-ccom.hoo.entity.Account结果如下:e)注意,有时候会出现异常信息,如:java.lang.NoSuchFieldException:JiBX_bindingXXXX就要重复下面的命令就可以了。java-cpbin;lib/jibx-bind.jarorg.jibx.binding.Compile-vbind.xml2、转换带List集合属性的JavaBeana)程序代码@Testpublicvoidl
本文标题:Jibx-处理XML
链接地址:https://www.777doc.com/doc-8637699 .html