妙用Commons良药<三>

阅读更多
这一篇文章主要是介绍怎样用Commons处理XMl文件
1、将XML文档转化为你想要的对象
   可以使用org.apache.commons.digester.Digester来实现你想要的功能,其步骤如下:定义待匹配的模式,以及遇到之些模式时将执行的动作,通过Digester可以很容易地实现你自己的SAX解析器,并让你无须处理复杂的SAX API,即可完成你想要的功能。Digester只是SAX外面的一个轻量级外壳,其解析的速度差不多和SAX一样快的。
   看一个XML文件:


  
    Hamlet
    William Shakespeare
    
      Prince of Denmark freaks out, talks to ghost, gets into a
      crazy nihilistic funk, and dies in a duel.
    
    
      
        Claudius
        King of Denmark
      
      
        Hamlet
         Son to the late, and nephew of the present king 
      
      
        Horatio
        friend to Hamlet 
      
    
  


为了解析这一个配置文件的内容,相对应地我们写出下面的类:
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的配置文件:


  
    
    
    
    
    
    
    
      
      
      
      
      
    
  


下面是其解析的过程,是不是相对于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 );



注:些文章中的代码来自<>第六章内容

你可能感兴趣的:(XML,Apache,Bean,IDEA)