使用Digester解析XML文件简单范例
2007-04-16 15:22
330 查看
需要读取的XML文件PayoutConfig.xml:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<pageList>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<payout_entry name="债项支用-授信品种和URL对照表">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="02001001" productName="土地储备贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="04001002" productName="房地产开发" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="03005001" productName="基本建设贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="01004002" productName="技术改造贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</payout_entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</pageList>
配置文件中item对应的类XmlVO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlVO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String productCode; //授信品种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String productName; // 授信品种名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getProductCode() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return productCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setProductCode(String productCode) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.productCode = productCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getProductName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return productName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setProductName(String productName) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.productName = productName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
建立XML文件解析类XmlConfig.java:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.commons.digester.Digester;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlVO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlConfig ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final String _PayoutPageConfig = "/com/xml/PayoutConfig.xml";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static Digester digester;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static XmlConfig instance = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final List PayoutElementVOList = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static XmlConfig getInstance() throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (instance == null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
instance = new XmlConfig();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(_PayoutPageConfig);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return instance;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static void init() throws IOException, SAXException, Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream is = XmlConfig.class.getResourceAsStream(_PayoutPageConfig);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester = new Digester();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.push(XmlConfig.getInstance());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.setValidating(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.setNamespaceAware(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addPayoutElement(digester, "pageList/payout_entry/item","addPayoutElementList");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.parse(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void addPayoutElement(Digester digester, String model,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String method) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addObjectCreate(model, XmlVO.class);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addSetProperties(model);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addSetNext(model, method);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addPayoutElementList(XmlVO payoutElement) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PayoutElementVOList.add(payoutElement);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List getPayoutElementList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return PayoutElementVOList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static List getPayoutElementVOList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return PayoutElementVOList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
现在就可以建立一个测试类来读取配置文件XmlTest.java:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlVO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlTest ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List payList = XmlConfig.getInstance().getPayoutElementVOList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < payList.size(); i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlVO payVO = (XmlVO) payList.get(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(payVO.getProductCode()+payVO.getProductName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (SAXException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
注意点:
1、必须把commons-beanutils-1.6.1.jar,commons-digester-1.5.jar,commons-collections-3.1.jar添加到编译路径中,否则会报错
2、XmlConfig.class.getResourceAsStream(_PayoutPageConfig)中XML文件的位置是以classes为根路径的。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<pageList>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<payout_entry name="债项支用-授信品种和URL对照表">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="02001001" productName="土地储备贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="04001002" productName="房地产开发" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="03005001" productName="基本建设贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<item productCode="01004002" productName="技术改造贷款" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</payout_entry>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</pageList>
配置文件中item对应的类XmlVO.java
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlVO ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String productCode; //授信品种
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String productName; // 授信品种名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getProductCode() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return productCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setProductCode(String productCode) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.productCode = productCode;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getProductName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return productName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setProductName(String productName) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.productName = productName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
建立XML文件解析类XmlConfig.java:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.ArrayList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.commons.digester.Digester;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlVO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlConfig ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final String _PayoutPageConfig = "/com/xml/PayoutConfig.xml";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static Digester digester;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static XmlConfig instance = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static final List PayoutElementVOList = new ArrayList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static XmlConfig getInstance() throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (instance == null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
instance = new XmlConfig();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(_PayoutPageConfig);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return instance;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private static void init() throws IOException, SAXException, Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream is = XmlConfig.class.getResourceAsStream(_PayoutPageConfig);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester = new Digester();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.push(XmlConfig.getInstance());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.setValidating(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.setNamespaceAware(false);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addPayoutElement(digester, "pageList/payout_entry/item","addPayoutElementList");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.parse(is);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static void addPayoutElement(Digester digester, String model,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String method) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addObjectCreate(model, XmlVO.class);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addSetProperties(model);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
digester.addSetNext(model, method);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addPayoutElementList(XmlVO payoutElement) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PayoutElementVOList.add(payoutElement);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public List getPayoutElementList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return PayoutElementVOList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static List getPayoutElementVOList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return PayoutElementVOList;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
现在就可以建立一个测试类来读取配置文件XmlTest.java:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.xml;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.List;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.xml.sax.SAXException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import com.xml.XmlVO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class XmlTest ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param args
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List payList = XmlConfig.getInstance().getPayoutElementVOList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < payList.size(); i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlVO payVO = (XmlVO) payList.get(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(payVO.getProductCode()+payVO.getProductName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (SAXException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
注意点:
1、必须把commons-beanutils-1.6.1.jar,commons-digester-1.5.jar,commons-collections-3.1.jar添加到编译路径中,否则会报错
2、XmlConfig.class.getResourceAsStream(_PayoutPageConfig)中XML文件的位置是以classes为根路径的。
相关文章推荐
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- 在已知XML文件内容时,使用dom4j简单解析XML(String类型的XML)(用于服务器端)
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- JAVA使用simpleframework简单的解析XML文件
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- Android中pull解析XML文件的简单使用
- 使用python对xml文件实现增删改查的简单封装
- iOSwebView解析pdf的简单使用
- 使用Apache的Digester来解析XML文档
- 在www.json.org上公布了很多Java下的json解析工具,其中org.json和json-lib比较简单,两者使用上差不多
- 解析Mysql备份与恢复简单总结与tee命令的使用介绍
- 使用Dom4j解析xml文件---DomReader方式
- 使用dom4j工具包对xml文件解析
- 使用PHP DOM-XML创建和解析XML文件
- Java XML解析之Digester的使用
- Debug命令使用解析及范例大全
- 解析xml文件和使用TreeView空间
- 用python来解析xml文件(简单情况)