1.源代码
2.要解析的xml文件
package com.zx.blog.common.util;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLParse {
public Map parseXML(String file){
Map<String,List> map=new HashMap<String,List>();
try{
SAXReader saxreader=new SAXReader();
Document document=saxreader.read(new File(file));
List<Element> dicList=document.selectNodes("/dictionary/dic");
System.out.println("dicList.size()="+dicList.size());
for(Element dic:dicList){
String dicType=dic.attributeValue("type");
System.out.println("dicType="+dicType);
List<String> dataList=new ArrayList<String>();
List<Element> wordList=dic.elements();
for(Element word:wordList){
String code=word.attributeValue("code");
String name=word.attributeValue("name");
String text=word.getTextTrim();
System.out.println("code="+code+" "+"name="+" "+name+" "+"text="+text);
dataList.add(code);
dataList.add(name);
}
map.put(dicType, dataList);
}
}catch(Exception e){
System.out.println("错误:"+e);
}
return map;
}
public static void main(String args[]){
XMLParse xml=new XMLParse();
Map map=xml.parseXML("d:\\dic.xml");
List<String> list=(List) map.get("OPType.opType");
for(String str:list){
System.out.println(str);
}
}
}
2.要解析的xml文件
<?xml version="1.0" encoding="GBK"?>
<dictionary>
<!--
配置约定:
<dic type="[名称:类名.属性]">
<word code="[编码:如0、1、2等,对应数据库中的值]"
name="[名称:用于显示的名称]">[备注信息]</word>
</dic>
-->
<dic type="Ne.neType">
<word code="A" name="内网">内网</word>
<word code="B" name="外网">外网</word>
</dic>
<dic type="Circuit.circuitType">
<word code="1" name="64K">电路类型1</word>
<word code="2" name="2M">电路类型2</word>
</dic>
<dic type="Port.portType">
<word code="1" name="CCSU"></word>
<word code="2" name="BSU"></word>
<word code="3" name="TUP"></word>
<word code="4" name="LDT"></word>
<word code="5" name="HPU"></word>
<word code="6" name="LPU"></word>
<word code="7" name="NDR"></word>
<word code="8" name="DIU"></word>
</dic>
<dic type="Port.takeState">
<word code="0" name="空闲"></word>
<word code="1" name="占用 "></word>
<word code="2" name="预占用"></word>
<word code="3" name="预删除"></word>
<word code="4" name="集团预留"></word>
<word code="5" name="集团占用"></word>
<word code="6" name="时钟"></word>
<word code="8" name="预分配"></word>
<word code="7" name="未安装"></word>
<word code="10" name="特殊用途"></word>
</dic>
<dic type="Port.portUseable">
<word code="0" name="不可用"></word>
<word code="1" name="可用 "></word>
<word code="2" name="端口坏 "></word>
</dic>
<dic type="Linkport.portUseable">
<word code="1" name="可用"></word>
<word code="0" name="不可用 "></word>
</dic>
<dic type="Linkport.portState">
<word code="0" name="空闲 "></word>
<word code="1" name="占用"></word>
<word code="4" name="集团占用 "></word>
<word code="5" name="集团预留"></word>
<word code="2" name="未安装"></word>
</dic>
<dic type="Linkport.portType">
<word code="1" name="CCSU"></word>
<word code="2" name="BSU"></word>
<word code="3" name="TUP"></word>
<word code="4" name="LDT"></word>
<word code="5" name="HPU"></word>
<word code="6" name="LPU"></word>
<word code="7" name="NDR"></word>
<word code="8" name="DIU"></word>
</dic>
<dic type="Linkport.linkPortType">
<word code="64K" name="64K"></word>
<word code="128K" name="128K"></word>
<word code="2M" name="2M"></word>
</dic>
<dic type="OPType.opType">
<word code="1" name="中继导入"></word>
<word code="2" name="信令导入"></word>
<word code="3" name="调单导入"></word>
<word code="4" name="网元导入"></word>
<word code="7" name="中继电路新增"></word>
<word code="8" name="中继电路修改"></word>
<word code="9" name="中继电路停闭"></word>
<word code="10" name="信令新增"></word>
<word code="11" name="信令修改"></word>
<word code="12" name="信令拆除"></word>
<word code="13" name="新增申请单"></word>
<word code="14" name="撤消申请单"></word>
<word code="15" name="修改申请单"></word>
<word code="16" name="提交申请单"></word>
<word code="17" name="中继端口新增"></word>
<word code="18" name="中继端口修改"></word>
<word code="19" name="中继端口删除"></word>
<word code="20" name="信令端口新增"></word>
<word code="21" name="信令端口修改"></word>
<word code="22" name="信令端口删除"></word>
<word code="23" name="批量导入新增信令"></word>
<word code="24" name="导入本地网调单"></word>
<word code="25" name="调单高级处理"></word>
<word code="26" name="申请单高级处理"></word>
<word code="27" name="网元新增"></word>
<word code="28" name="网元修改"></word>
<word code="29" name="网元删除"></word>
<word code="30" name="网元现网采集"></word>
<word code="31" name="导入申请单"></word>
</dic>
<dic type="NePort.server_Signal_Seq">
<word code="1" name="第一信令点"></word>
<word code="2" name="第二信令点"></word>
<word code="3" name="第三信令点"></word>
<word code="4" name="第四信令点"></word>
</dic>
</dictionary>