这一篇文章主要是介绍怎样用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>>第六章内容
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver