1. xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?> <!--字典数据配置--> <zdDatas> <zdData> <zdTableName>BDC_XT_BH</zdTableName> <zdTableComment>编号配置表</zdTableComment> <zdComments> <zdComment> <zdColName>BHID</zdColName> <zdColComment>列注释1</zdColComment> </zdComment> <zdComment> <zdColName>BH</zdColName> <zdColComment>列注释2</zdColComment> </zdComment> <zdComment> <zdColName>BHLXDM</zdColName> <zdColComment>列注释3</zdColComment> </zdComment> <zdComment> <zdColName>LSHWS</zdColName> <zdColComment>列注释4</zdColComment> </zdComment> </zdComments> </zdData> <zdData> <zdTableName>BDC_XT_GD</zdTableName> <zdTableComment>无描述</zdTableComment> <zdComments> <zdComment> <zdColName>ID</zdColName> <zdColComment>BDC_XT_GD列注释1</zdColComment> </zdComment> <zdComment> <zdColName>DM</zdColName> <zdColComment>BDC_XT_GD列注释2</zdColComment> </zdComment> <zdComment> <zdColName>GDLX</zdColName> <zdColComment>BDC_XT_GD列注释3</zdColComment> </zdComment> <zdComment> <zdColName>GDMBMC</zdColName> <zdColComment>BDC_XT_GD列注释4</zdColComment> </zdComment> </zdComments> </zdData> </zdDatas>
2. 读取文件:
public static void main(String[] args) { try { ClassPathResource classPathResource = new ClassPathResource("/META-INF/conf/server/zdDataConfig.xml"); File file = classPathResource.getFile(); SAXReader reader = new SAXReader(); Document document = reader.read(file); Element rootElement = document.getRootElement(); Element footElement; Element footElement3; for (Iterator i = rootElement.elementIterator("zdData");i.hasNext();){ System.out.println("-------------begin------------"); footElement = (Element) i.next(); String zdTableName = footElement.elementText("zdTableName"); System.out.println("字典名称:"+zdTableName); String zdTableComment = footElement.elementText("zdTableComment"); System.out.println("字典名称注释:"+zdTableComment); Element zdComment2 = footElement.element("zdComments"); //获取当前某个节点作为下一次的父节点 for (Iterator j = zdComment2.elementIterator("zdComment");j.hasNext();){ footElement3 = (Element) j.next(); String zdColName = footElement3.elementText("zdColName"); System.out.println("字典列名:"+zdColName); String zdColComment = footElement3.elementText("zdColComment"); System.out.println("字典列名注释:"+zdColComment); } System.out.println("-----------over-----------"); } } catch (Exception e) { e.printStackTrace(); } }
3. 结果
-------------begin------------ 字典名称:BDC_XT_BH 字典名称注释:编号配置表 字典列名:BHID 字典列名注释:列注释1 字典列名:BH 字典列名注释:列注释2 字典列名:BHLXDM 字典列名注释:列注释3 字典列名:LSHWS 字典列名注释:列注释4 -----------over----------- -------------begin------------ 字典名称:BDC_XT_GD 字典名称注释:无描述 字典列名:ID 字典列名注释:BDC_XT_GD列注释1 字典列名:DM 字典列名注释:BDC_XT_GD列注释2 字典列名:GDLX 字典列名注释:BDC_XT_GD列注释3 字典列名:GDMBMC 字典列名注释:BDC_XT_GD列注释4 -----------over----------- Process finished with exit code 0