`
lighter
  • 浏览: 500287 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

妙用Commons良药<三>

阅读更多
这一篇文章主要是介绍怎样用Commons处理XMl文件
1、将XML文档转化为你想要的对象
   可以使用org.apache.commons.digester.Digester来实现你想要的功能,其步骤如下:定义待匹配的模式,以及遇到之些模式时将执行的动作,通过Digester可以很容易地实现你自己的SAX解析器,并让你无须处理复杂的SAX API,即可完成你想要的功能。Digester只是SAX外面的一个轻量级外壳,其解析的速度差不多和SAX一样快的。
   看一个XML文件:
<?xml version="1.0"?>
<plays>
  <play genre="tragedy" year="1603" language="english">
    <name>Hamlet</name>
    <author>William Shakespeare</author>
    <summary>
      Prince of Denmark freaks out, talks to ghost, gets into a
      crazy nihilistic funk, and dies in a duel.
    </summary>
    <characters>
      <character protagonist="false">
        <name>Claudius</name>
        <description>King of Denmark</description>
      </character>
      <character protagonist="true">
        <name>Hamlet</name>
        <descr> Son to the late, and nephew of the present king </descr>
      </character>
      <character protagonist="false">
        <name>Horatio</name>
        <descr>friend to Hamlet </descr>
      </character>
    </characters>
  </play>
</plays>


为了解析这一个配置文件的内容,相对应地我们写出下面的类:
public class Play {
    private String genre;
    private String year;
    private String language;
    private String name;
    private String author;
    private String summary;
    private List characters = new ArrayList( );
    // accessors omitted for brevity
    // Add method to support adding elements to characters.
    public void addCharacter(Character character) {
        characters.add( character );
    }
}
public class Character {
    private String name;
    private String description;
    private boolean protagonist;
    // accessors omitted for brevity
}


下一步,写出Digester的配置文件:
<?xml version="1.0"?>
<digester-rules>
  <pattern value="plays/play">
    <object-create-rule classname="xml.digester.Play"/>
    <set-next-rule methodname="add" paramtype="java.lang.Object"/>
    <set-properties-rule/>
    <bean-property-setter-rule pattern="name"/>
    <bean-property-setter-rule pattern="summary"/>
    <bean-property-setter-rule pattern="author"/>
    <pattern value="characters/character">
      <object-create-rule classname="xml.digester.Character"/>
      <set-next-rule methodname="addCharacter" 
                     paramtype="xml.digester.Character"/>
      <set-properties-rule/>
      <bean-property-setter-rule pattern="name"/>
      <bean-property-setter-rule pattern="descr" 
                                 propertyname="description"/>
    </pattern>
  </pattern>
</digester-rules>


下面是其解析的过程,是不是相对于SAX来说简单多了啊,呵呵:
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;

List plays = new ArrayList( );
// Create an instance of the Digester from the XML rule set
URL rules = getClass( ).getResource("./play-rules.xml");
Digester digester = DigesterLoader.createDigester(rules);
// Push a reference to the plays List on to the Stack
digester.push(plays);

// Parse the XML document
InputStream input = getClass( ).getResourceAsStream("./plays.xml");
Object root = digester.parse(input);

// The XML document contained one play "Hamlet"
Play hamlet = (Play) plays.get(0);
List characters = (List) hamlet.getCharacters( );



2、将Beans转化为XML文档
使用Commons Betwixt里面的org.apache.commons.betwixt.io.BeanWriter类可以把某个bean转换为一个xml文档,看下面的例子:

Play play = new Play( );
// populatePlay populates all properties and nested Character objects
populatePlay( play );
// Write XML document
BeanWriter beanWriter = new BeanWriter( );
beanWriter.enablePrettyPrint( );
beanWriter.write( play );
System.out.println( beanWriter.toString( ) );

短短的几行代码就可以实现这一个功能了,简单吧;注意一下,beanWriter.enablePrettyPrint( )是启用缩进格式.

BeanWriter 类还用于向某个OutputStream或Writer写入XML文档,只要向其构造函数传一个Writer或OutputStream实例即可.
import org.apache.commons.betwixt.io.BeanWriter

Play play = new Play( );
populatePlay( play );

Writer outputWriter = new FileWriter("test.dat");
BeanWriter beanWriter = new BeanWriter( outputWriter );
beanWriter.setEndOfLine( "\r\n" );
beanWriter.setIndent( "\t" );
beanWriter.enablePrettyPrint( );
beanWriter.write( play );
outputWriter.close( );

另外提示一下,beanWriter.setEndOfLine()方法可接受一个String作为行结束序列。beanWriter.setIndent()方法接受一个String参数,该String将在BeanWriter中enablePrettyPrint()开启缩进时用作缩进字符串。

3、转换XML文档为Beans
其实这一部分的内容和第一部分的内容差不多有点类似的,有兴趣的朋友可以对比一下.
Commons Betwixt使用Commons Digester解析XML,而BeanReader即为Digester的一个子类。BeanReader使用自省机制以及类路径中的Betwixt映射文件来创建了一组Digester规则.
举例,有一个XMl文件就为前面第一个XML文件

import org.apache.commons.betwixt.io.BeanReader;

InputStream customPlay = 
    getClass( ).getResourceAsStream("./customized-play.xml");
BeanReader beanReader = new BeanReader( );
beanReader.getXMLIntrospector( ).setWrapCollectionsInElement(false);
beanReader.registerBeanClass(Play.class);
Play play = (Play) beanReader.parse( customPlay );



注:些文章中的代码来自<<Jakarta Commons Cookbook>>第六章内容
分享到:
评论
1 楼 ming_7755 2011-02-22  
       

相关推荐

    Springboot集成Ueditor详细文档说明

    &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;commons-io&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.5&lt;/version&gt; &lt;/...

    Apache Commons 所有包最新版本 含SRC (6/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (5/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (1/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (3/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (7/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    Apache Commons 所有包最新版本 含SRC (4/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    aspose-words-15.8.0-jdk16.jar.rar

    添加依赖: &lt;dependency&gt; &lt;groupId&gt;com.itextpdf&lt;/groupId&gt; &lt;artifactId&gt;itext7-core... &lt;groupId&gt;org.apache.commons&lt;/groupId&gt; &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;1.3.2&lt;/version&gt; &lt;/dependency&gt;

    Apache Commons 所有包最新版本 含SRC (2/7)

    commons-attributes-2.2-src.zip&lt;br&gt;commons-attributes-2.2.zip&lt;br&gt;commons-beanutils-1.8.0-BETA-src.zip&lt;br&gt;commons-beanutils-1.8.0-BETA.zip&lt;br&gt;commons-betwixt-0.8-src.zip&lt;br&gt;commons-betwixt-0.8.zip&lt;br&gt;...

    client发送短信验证码(云之讯)

    &lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt; &lt;version&gt;3.0&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;commons-lang&lt;/groupId&gt; &lt;artifactId&gt;commons-lang&lt;/artifactId&gt; &lt;version&gt;2.6&lt;/version&gt; &lt;/...

    基于springboot演示resuful api、mock请求、validate验证、异常捕捉、aop切面编程+源代码+文档说

    &lt;version&gt;${commons-lang3.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.projectlombok&lt;/groupId&gt; &lt;artifactId&gt;lombok&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.spring...

    java实现poi 在线预览,excel,word直接在页面显示,附带文件上传,多文件上传

    &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt; &lt;version&gt;4.1&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.poi&lt;/groupId&gt; &lt;artifactId&gt;poi&lt;/artifactId&gt; &lt;version&gt;3.17&lt;/version&gt; &lt;/...

    struts框架jar包

    struts-1.3.8 包(&lt;br&gt;&lt;br&gt;antlr-2.7.2.jar&lt;br&gt;bsf-2.3.0.jar&lt;br&gt;commons-beanutils-1.7.0.jar&lt;br&gt;commons-chain-1.1.jar&lt;br&gt;commons-digester-1.8.jar&lt;br&gt;commons-fileupload-1.1.1.jar&lt;br&gt;commons-io-1.1.jar&lt;br&gt;...

    Java 课程设计 高校公寓管理系统

    &lt;artifactId&gt;standard&lt;/artifactId&gt; &lt;version&gt;1.1.2&lt;/version&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;

    Java 登录注册案例实例

    &lt;artifactId&gt;commons-io&lt;/artifactId&gt; &lt;version&gt;2.11.0&lt;/version&gt; &lt;artifactId&gt;mybatis&lt;/artifactId&gt; &lt;version&gt;3.5.5&lt;/version&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;8.0.28&lt;/version&gt; ...

    JSF与Shale开发用包

    其中包含:shale-core.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-chain.jar&lt;br&gt;commons-codec.jar&lt;br&gt;commons-collections.jar&lt;br&gt;commons-digester.jar&lt;br&gt;commons-el.jar&lt;br&gt;commons-fileupload.jar&lt;br&gt;commons-...

    springcloud+springcloud alibaba完整学习项目

    &lt;module&gt;cloud-api-commons&lt;/module&gt; &lt;module&gt;cloud-eureka-server7001&lt;/module&gt; &lt;module&gt;cloud-eureka-server7002&lt;/module&gt; &lt;module&gt;cloud-comsumer-order80&lt;/module&gt; &lt;module&gt;cloud-provider-payment8001&lt;/...

    spring-hibernate-dwr实例

    collections-2.1.1.jar&lt;br&gt;commons-logging-1.0.4.jar&lt;br&gt;dom4j-1.6.1.jar&lt;br&gt;ehcache-1.1.jar&lt;br&gt;hibernate3.jar&lt;br&gt;jaas.jar&lt;br&gt;jaxen-1.1-beta-7.jar&lt;br&gt;jdbc2_0-stdext.jar&lt;br&gt;jta.jar&lt;br&gt;log4j-1.2.11.jar&lt;br&gt;...

    spring+struts+hibernate+dwr+jstl做的实例

    api.jar&lt;br&gt;jxl.jar&lt;br&gt;itext-1.3.jar&lt;br&gt;poi-2.5.1.jar&lt;br&gt;cos.jar&lt;br&gt;velocity-1.5.jar&lt;br&gt;velocity-tools-view-1.3.jar&lt;br&gt;commons-codec.jar&lt;br&gt;antlr.jar&lt;br&gt;commons-beanutils.jar&lt;br&gt;commons-digester.jar&lt;br...

    ssm的jar包

    ssm在maven项目管理下的pom文件,jar包依赖 &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt; ... &lt;artifactId&gt;commons-codec&lt;/artifactId&gt; &lt;version&gt;1.10&lt;/ver

Global site tag (gtag.js) - Google Analytics