欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

解析XML文件,并封装成ARRAYLIST,xmlarraylist

来源: javaer 分享于  点击 16407 次 点评:5

解析XML文件,并封装成ARRAYLIST,xmlarraylist


/**
* 加载指定文件
*
* @param configFileName
* @throws Exception
* @return FileInputStream
*/
public static FileInputStream loadConfig(String configFileName) throws Exception {




File configFile = null;
FileInputStream input = null;
try {
// 检测指定文件名的有效性
if (configFileName == null || configFileName.trim().length() <= 0) {
Log.print("configFileName is null");
throw new IException("Gen_E001");
}


configFile = new File(configFileName);


//Log.print("load bs config file:" + configFile.getAbsolutePath());


input = new FileInputStream(configFile);


}
catch(IException ie)
{
throw ie;
}
catch(Exception e)
{
Log.print(e.toString());
throw new IException("Gen_E001");
}


return input;
}

/**
* 将xml数据流转换为DOM模型对象
*
* @param input
* @return Document
* @throws Exception
*/
public static Document  parse(InputStream input, String encoding) throws Exception {
DOMParser parser = new DOMParser();


try {
if (input == null) {
Log.print("null InputStream");
throw new IException("Gen_E001");
}


InputSource source = new InputSource(input);


if (encoding != null && !"".equals(encoding))
source.setEncoding(encoding);
parser.parse(source);


}
catch(IException ie)
{
throw ie;
}
catch(Exception e)
{
Log.print(e.toString());
e.printStackTrace();
throw new IException("Gen_E001");
}


return parser.getDocument();
}


//加载解析表集合XML文件
public ArrayList marshalTable(Node xmlDoc) throws Exception
{
ArrayList arrayList = null;
//该 Node 接口是整个文档对象模型的主要数据类型
if (xmlDoc == null)
{
Log.print("xmlDoc is null");
throw new IException("Gen_E001");
}


arrayList = traverseTable(xmlDoc, null);

return arrayList;
}

//将响应返回的XML文件解析成对象
private ArrayList traverseTable(Node node, ConfigTableInfo tableInfo) throws Exception
{
//该 Node 接口是整个文档对象模型的主要数据类型
//表示基础对象的类型的节点
short type = node.getNodeType();
switch (type)
{
case Node.DOCUMENT_NODE :  //该节点为 Document
{
//public interface Documentextends Node
//Document 接口表示整个 HTML 或 XML 文档。从概念上讲,它是文档树的根,并提供对文档数据的基本访问
Document document = (Document) node;

//这是一种便捷属性,该属性允许直接访问文档的文档元素的子节点
traverseTable(document.getDocumentElement(), tableInfo);
break;
}


case Node.ELEMENT_NODE :  //该节点为 Element
{
//此节点的第一个子节点
Node child = node.getFirstChild();

while (child != null)
{
//此节点的名称,取决于其类型 child.getNodeName()
if ("itreasury_configitem".equals(child.getNodeName()))
{ //如果是一个账户信息
tableInfo = new ConfigTableInfo(); //new 一个新的信息类
traverseTable(child, tableInfo); //结息账户信息

resultTableSet.add(tableInfo);
tableInfo = null;
}
else
{
traverseTable(child, tableInfo);
}


//直接在此节点之后的节点
child = child.getNextSibling();
}
break;
}
case Node.TEXT_NODE :  //该节点为 Text 节点
{
/**
* 如果账户交易信息AccountTransactionInfo对象不为null

* 则检测是否存在账户明细交易信息节点,存在则赋值
*/


//此节点的父节点 node.getParentNode()
//此节点的名称,取决于其类型 getNodeName()
if (tableInfo != null && "name".equals(node.getParentNode().getNodeName()))
{
//此节点的值,取决于其类型
tableInfo.setName(node.getNodeValue());
}
else if (tableInfo != null && "tablename".equals(node.getParentNode().getNodeName()))
{
tableInfo.setTablename(node.getNodeValue());
}
else if (tableInfo != null && "desc".equals(node.getParentNode().getNodeName()))
{
tableInfo.setDesc(node.getNodeValue());
}
else if (tableInfo != null && "value".equals(node.getParentNode().getNodeName()))
{
tableInfo.setVale(node.getNodeValue());
}


break;
}
}

return resultSet;
}

void main(){

ArrayList result = new ArrayList();

String configTablePath="";//xml文件路径

FileInputStream fileInput = loadConfig(configTablePath);

//所有超级接口: Node
//Document 接口表示整个 HTML 或 XML 文档。从概念上讲,它是文档树的根,并提供对文档数据的基本访问
//将xml数据流转换为DOM模型对象
Document node = parse(fileInput);

//将Dom模型反解析成响应对象
result = marshalTable(node);


}

相关文章

    暂无相关文章

用户点评