apache.commoms.digester3 解析xml文件

1 简介

java解析xml,就个人所知有3种方法DOM、SAX和Digester。前两种需要编写大量代码,且不利于代码移植和复用;

Digester解析xml又有两种方式,代码和xmlrule。xmlrule方式读取规定格式的xml解析规则文件,将标签和属性值赋值到指定的Java POJO对象。

2 待解析的xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="uhhe.xsl"?>
<GEOBOUND>
<GEOPT X="1" Y="1" Z="1">
  <X SIZE="1" TYPE="5">10.00000000</X>
  <Y SIZE="1" TYPE="5">10.00000000</Y>
  <Z SIZE="1" TYPE="5">10.00000000</Z>
</GEOPT>
<GEOPT X="1" Y="1" Z="1">
  <X SIZE="1" TYPE="5">20.00000000</X>
  <Y SIZE="1" TYPE="5">20.00000000</Y>
  <Z SIZE="1" TYPE="5">20.00000000</Z>
</GEOPT>
<GEOPT X="1" Y="1" Z="1">
  <X SIZE="1" TYPE="5">30.00000000</X>
  <Y SIZE="1" TYPE="5">30.00000000</Y>
  <Z SIZE="1" TYPE="5">30.00000000</Z>
</GEOPT>
<GEOPT X="1" Y="1" Z="1">
  <X SIZE="1" TYPE="5">40.00000000</X>
  <Y SIZE="1" TYPE="5">40.00000000</Y>
  <Z SIZE="1" TYPE="5">40.00000000</Z>
</GEOPT>
</GEOBOUND>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

3 解析规则文件

<?xml version='1.0'?>
<!DOCTYPE digester-rules PUBLIC
  "-//Apache Commons //DTD digester-rules XML V1.0//EN"
  "http://commons.apache.org/digester/dtds/digester-rules-3.0.dtd">
    <digester-rules>
        <pattern value="GEOBOUND">
            <object-create-rule classname="xml.digester.Bound" /> <!-- 创建对象 -->
            <set-properties-rule /> <!-- 设置属性 -->
            <pattern value="GEOPT">
                <object-create-rule classname="xml.digester.GeoPt" /> <!-- 创建对象 -->
                <bean-property-setter-rule pattern="X" propertyname="dX"/>
                <bean-property-setter-rule pattern="Y" propertyname="dY"/>
                <bean-property-setter-rule pattern="Z" propertyname="dZ"/>
                <set-next-rule methodname="addPt" paramtype="xml.digester.GeoPt"/>
            </pattern>
        </pattern>
    </digester-rules>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

4 存贮解析结果的Java POJO

package xml.digester;
import java.util.Vector;
public class Bound{
    private Vector<GeoPt> pts = new Vector<GeoPt>();
    public void addPt(GeoPt pt){
        pts.add(pt);
    }
    public Vector<GeoPt> getPt(){
        return pts;
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

package xml.digester;

public class GeoPt{

    public double dX;
    public double dY;
    public double dZ;
    public double getdX() {
        return dX;
    }
    public void setdX(double dX) {
        this.dX = dX;
    }
    public double getdY() {
        return dY;
    }
    public void setdY(double dY) {
        this.dY = dY;
    }
    public double getdZ() {
        return dZ;
    }
    public void setdZ(double dZ) {
        this.dZ = dZ;
    }    

}
注意
  • 类成员变量名不可为X、Y、Z,否则digester报错,原因不明。
  • 可以先定义成员变量,然后用MyEclipse工具“生成setter和getter函数”

 

5 xmlrule文件加载类

package xml.digester;

import java.io.File;

import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;

public class myRulesLoader extends FromXmlRulesModule
{
    private String xmlrule;

    public myRulesLoader(String xmlfile){
        xmlrule = xmlfile;
    }
    @Override
    protected void loadRules()
    {
        loadXMLRules( new File( xmlrule ) );
    }

}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

6 运行示例

public static void main(String[] args){
    try {
    // 加载xmlrule文件
    DigesterLoader loader = newLoader(new myRulesLoader("d:\\drule3.xml"));
    // 创建Digester实例
    Digester digester = loader.newDigester();
    // 解析xml文件
    FileInfo info = (FileInfo) digester.parse(new File("d:\\9.xml"));
    // 分析解析结果
    info.show();
    }
    catch (Exception e){
        e.printStackTrace();
    }

}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

7 结果示意

...
Name: Band_1KM_Emissive
    Fname: G:\YX\HDF_NC_H5\MOD021KM.A2009345.0400.005.2010257180913.hdf
    Description:    1KM Emissive Band Numbers for Subsetting
    NS:   16
    NL:   1
    NB:   1
    FileType:    HDF4
    DataType:    FLOAT
    Satellite:  null
    Sensor:  null
     picPath:
     xyz:     0.00.00.0
     xyz:     0.00.00.0
     xyz:     0.00.00.0
     xyz:     0.00.00.0
...

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上一篇:跟我一起学.NetCore之中间件(Middleware)简介和解析请求管道构建


下一篇:2015.07.20MapReducer源码解析(笔记)