需要读取的XML文件PayoutConfig.xml:
<pageList>
<payout_entry name="债项支用-授信品种和URL对照表">
<item productCode="02001001" productName="土地储备贷款" />
<item productCode="04001002" productName="房地产开发" />
<item productCode="03005001" productName="基本建设贷款" />
<item productCode="01004002" productName="技术改造贷款" />
</payout_entry>
</pageList>配置文件中item对应的类XmlVO.java
package com.xml;

public class XmlVO ...{
private String productCode; //授信品种
private String productName; // 授信品种名称
public String getProductCode() ...{
return productCode;
}
public void setProductCode(String productCode) ...{
this.productCode = productCode;
}
public String getProductName() ...{
return productName;
}
public void setProductName(String productName) ...{
this.productName = productName;
}
}建立XML文件解析类XmlConfig.java:
package com.xml;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
import com.xml.XmlVO;

public class XmlConfig ...{
private static final String _PayoutPageConfig = "/com/xml/PayoutConfig.xml";
private static Digester digester;
private static XmlConfig instance = null;
private static final List PayoutElementVOList = new ArrayList();

public static XmlConfig getInstance() throws Exception ...{
if (instance == null) ...{
instance = new XmlConfig();
System.out.println(_PayoutPageConfig);
init();
}
return instance;
}

private static void init() throws IOException, SAXException, Exception ...{
InputStream is = XmlConfig.class.getResourceAsStream(_PayoutPageConfig);
digester = new Digester();
digester.push(XmlConfig.getInstance());
digester.setValidating(false);
digester.setNamespaceAware(false);
addPayoutElement(digester, "pageList/payout_entry/item","addPayoutElementList");
digester.parse(is);
}
private static void addPayoutElement(Digester digester, String model,
String method) ...{
digester.addObjectCreate(model, XmlVO.class);
digester.addSetProperties(model);
digester.addSetNext(model, method);
}

public void addPayoutElementList(XmlVO payoutElement) ...{
PayoutElementVOList.add(payoutElement);
}

public List getPayoutElementList() ...{
return PayoutElementVOList;
}

public static List getPayoutElementVOList() ...{
return PayoutElementVOList;
}
}
现在就可以建立一个测试类来读取配置文件XmlTest.java:
package com.xml;
import java.util.List;
import org.xml.sax.SAXException;
import com.xml.XmlConfig;
import com.xml.XmlVO;

public class XmlTest ...{

/** *//**
* @param args
*/
public static void main(String[] args) ...{
try ...{
List payList = XmlConfig.getInstance().getPayoutElementVOList();
for (int i = 0; i < payList.size(); i++) ...{
XmlVO payVO = (XmlVO) payList.get(i);
System.out.println(payVO.getProductCode()+payVO.getProductName());
}

} catch (SAXException e) ...{
System.out.print(e.getMessage());
} catch (Exception e) ...{
System.out.print(e.getMessage());
}
}
}
注意点:
1、必须把commons-beanutils-1.6.1.jar,commons-digester-1.5.jar,commons-collections-3.1.jar添加到编译路径中,否则会报错
2、XmlConfig.class.getResourceAsStream(_PayoutPageConfig)中XML文件的位置是以classes为根路径的。
XML配置文件解析
本文介绍了一个使用Java进行XML配置文件解析的例子。通过定义XmlVO类存储XML数据,并使用Digester进行解析,最后通过测试类展示解析结果。
1280

被折叠的 条评论
为什么被折叠?



