Digester使用之:Digester映射一例通

digester是一个经常使用的工具。用来将XML文件转换为JAVA对象。

本文通过一个例子来说明digester映射规则文件的写法。

需要转换的XML文件
<server>
    <files>
        <file Speed="0.01" Progress="0.19">fileName1.txt</file>
	<file Speed="0.01" Progress="0.00">fileName2.txt</file>
    </files>
</server>


对应的类
public class FileStatus {
    private String fileName;
    private String speed;
    private String progress;
	
    public String getFileName ( ) {
	return fileName;
    }
    public void setFileName ( String fileName ) {

	this.fileName = fileName;
    }
    public String getSpeed ( ) {
        return speed;
    }
    public void setSpeed ( String speed ) {
	this.speed = speed;
    }
    public String getProgress ( ) {
        return progress;
    }
    public void setProgress ( String progress ) {
	this.progress = progress;
    }
}


映射文件
<?xml version="1.0"?>
<digester-rules>
    <pattern value="server/files/file">
        <object-create-rule classname="lab.FileStatus"/>
        <set-next-rule methodname="add" paramtype="java.lang.Object"/>
        <set-properties-rule>
                <alias attr-name="Speed" prop-name="speed" />
                <alias attr-name="Progress" prop-name="progress" />
        </set-properties-rule>
        <bean-property-setter-rule propertyname="fileName"/>
    </pattern>
</digester-rules>


由上例可以知道:

映射属性用:
         <set-properties-rule>
                <alias attr-name="Speed" prop-name="speed" />
        </set-properties-rule>

映射元素用:
         <bean-property-setter-rule propertyname="fileName"/>
        或
         <bean-property-setter-rule pattern="" propertyname="fileName"/>

你可能感兴趣的:(java,xml,asp)