XML的TreeConfig

本文介绍了一个基于XML配置文件的解析过程,通过Java实现了一套解析工具,能够读取特定格式的XML文件并将其转换为内存中的数据结构。该工具主要用于解析包含节点层级结构的配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* <?xml version="1.0" encoding="UTF-8"?>
<root>
<tree id="root" useArrows="false">
<node>
<id>unifyProxy</id>
<parentId></parentId>
<text>开通接口</text>
<method></method>
</node>
<node expanded="false">
<id>corpManager</id>
<parentId>unifyProxy</parentId>
<text>集团管理</text>
<method></method>
</node>
<node>
<id>corp</id>
<parentId>corpManager</parentId>
<text>集团</text>
<method>initBusinessGroup()</method>
</node>
</tree>
</root>

TreeNode:
private String id;

private String parentId;

private String text;

private String method;

Tree:
private String id = null;

private List<TreeNode> nodeList = new ArrayList<TreeNode>();


*/
private static final Map<String, Tree> treeMap = new HashMap<String, Tree>();

private String nodeConfigFile;

public static Map<String, Tree> getTreeMap()
{
return treeMap;
}

/**
* 读取配置文件
*/
public void init()
{
try
{
// String cmClientPath = SystemLoader.getCmClientPath();
// File configFile = new File(cmClientPath + File.separator + nodeConfigFile.trim());
File configFile = new File("D://我的文档//workspace/CMClient//WebRoot//WEB-INF//conf//common//tree_config.xml");

SAXReader reader = new SAXReader();
Document document = reader.read(configFile);
parse(document);

}
catch (DocumentException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* 解析配置值
* @param document
* @author liudong
* @throws Exception 抛出异常
*/
private void parse(Document document)
throws Exception
{
//Tree就是
Tree tree = null;
TreeNode treeNode = null;
final Element root = document.getRootElement();
final List< ? > trees = root.elements("tree");
for (final Iterator< ? > iterator = trees.iterator(); iterator.hasNext();)
{
Element treeElement = (Element) iterator.next();

tree = new Tree();
parseTreeAttribute(tree, treeElement);

List< ? > nodes = treeElement.elements("node");
for (final Iterator< ? > iter = nodes.iterator(); iter.hasNext();)
{

final Element nodeElement = (Element) iter.next();

treeNode = new TreeNode();

parseNodeAttribute(treeNode, nodeElement);

parseNodeValue(treeNode,nodeElement);

tree.getNodeList().add(treeNode);
}
treeMap.put(tree.getId(), tree);
}
}

private void parseTreeAttribute(Tree tree, Element element)
{

//
final Attribute idAtt = element.attribute("id");
tree.setId("root");
if (null != idAtt && ParamChecking.isBlank(idAtt.getValue()))
{
tree.setId(UnifyUtils.processTrim(idAtt.getValue()));
}

final Attribute useArrowsAtt = element.attribute("useArrows");
if (null != useArrowsAtt && "false".equals(UnifyUtils.processTrim(useArrowsAtt.getValue())))
{
tree.getTreeOptions().setUseArrows(false);
}

}

private void parseNodeAttribute(TreeNode node, Element element)
{
final Attribute expanded = element.attribute("expanded");
if (null != expanded && "false".equals(expanded.getValue()))
{
node.getTreeNodeOptions().setExpanded(false);
}

}

private void parseNodeValue(TreeNode node, Element element)
{
org.dom4j.Node idNode = element.element("id");
org.dom4j.Node textNode = element.element("text");
org.dom4j.Node parentIdNode = element.element("parentId");
org.dom4j.Node methodNode = element.element("method");

node.setId(idNode.getText().trim());
node.setParentId(parentIdNode.getText().trim());
node.setText(textNode.getText().trim());
node.setMethod(methodNode.getText().trim());
}

/**
* @return Returns the nodeConfigFile.
*/
public String getNodeConfigFile()
{
return nodeConfigFile;
}

/**
* @param nodeConfigFile The nodeConfigFile to set.
*/
public void setNodeConfigFile(String nodeConfigFile)
{
this.nodeConfigFile = nodeConfigFile;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值