Java 解析XML 之 DOM4J 解析 XML 分析测试

DOM4J 解析 XML 测试代码,

测试环境: JDK 6

Add jar:  dom4j-1.6.jar  、jaxen-1.1-beta-6.jar(xpath 支持)

 

 

import java.io.File; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DOM4JParseService { // 从文件读取XML,输入文件名,返回XML文档 public static Document read(String fileName) throws MalformedURLException, DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName)); return document; } public static void main(String[] args) { Document doc = null; try { doc = read("D://profile.xml"); } catch (Exception e) { e.printStackTrace(); } List<AFile> files = getAFileList(doc); System.out.println("files.size():-----" + files.size()); for (AFile file : files) { System.out.println(file.toString()); } DBConfig dBConfig = getDBConfig(doc); System.out.println(dBConfig.toString()); getWLMInfo(doc); } public static List getAFileList(Document doc) { List filelist = doc.selectNodes("root/IOInfo/files/file"); List<AFile> afiles = new ArrayList<AFile>(); for (Element file : (List<Element>) filelist) { AFile afile = new AFile(); afile.setCategory(file.attributeValue("cate")); afile.setIn(file.elementText("in")); afile.setOut(file.elementText("out")); afile.setAwk(file.elementText("awk")); afiles.add(afile); } return afiles; } public static DBConfig getDBConfig(Document doc) { Element xmldbconfig = (Element) doc.selectNodes("root/DBConnection").get(0); DBConfig dBConfig = new DBConfig(); dBConfig.setIp(xmldbconfig.elementText("location")); dBConfig.setDbname(xmldbconfig.elementText("database")); dBConfig.setDbuser(xmldbconfig.elementText("userName")); dBConfig.setDbpassword(xmldbconfig.elementText("password")); return dBConfig; } public static void getWLMInfo(Document doc) { List wLMInfoList = doc.selectNodes("root/WLMInfo/WLMs"); // List<AFile> afiles = new ArrayList<AFile>(); System.out.println("==================WLMInfo========================="); for (Element wLMInfo : (List<Element>) wLMInfoList) { System.out.println(wLMInfo.attributeValue("tp")); for (Iterator i = wLMInfo.elementIterator("name"); i.hasNext();) { Element name = (Element) i.next(); System.out.println("name: " + name.getTextTrim()); } System.out.println("-------------------"); } System.out.println("=================WLMInfo=========================="); } }

 

 

XML 文件:profile.xml

<root> <DBConnection> <location>127.0.0.1</location> <database>abcdb</database> <userName>sa</userName> <password>1234</password> </DBConnection> <options> <ifExtract>yes</ifExtract> <ifImport>yes</ifImport> </options> <IOInfo> <folders> <ImportFolder>E:/....</ImportFolder> <inFolder>C:/....</inFolder> <outFolder>D:/....</outFolder> </folders> <files> <file cate="sumy"> <in>PPRMFSUMY</in> <out>sumy.txt</out> <awk>sumy.awk</awk> </file> <file cate="cpu"> <in>PPRMFCPUALL</in> <out>cpu.txt</out> <awk>cpu.awk</awk> </file> <file cate="wlm"> <in>PPRMFWLMALL</in> <out>wlm.txt</out> <awk>wlm.awk</awk> </file> <file cate="vstor"> <in>PPRMFVSTOR</in> <out>vstor.txt</out> <awk>vstor.awk</awk> </file> <file cate="paging"> <in>PPRMFPAGING</in> <out>paging.txt</out> <awk>page.awk</awk> </file> <file cate="cfSum"> <in>PPRMFCF1</in> <out>cfSum.txt</out> <awk>cfSum.awk</awk> </file> <file cate="cfSubCh"> <in>PPRMFCF1</in> <out>cfSubCh.txt</out> <awk>cfSubCh.awk</awk> </file> <file cate="cfStrBa"> <in>PPRMFCF1</in> <out>cfStrBa.txt</out> <awk>cfStrBa.awk</awk> </file> <file cate="cfStrEx"> <in>PPRMFCF1</in> <out>cfStrEx.txt</out> <awk>cfStrEx.awk</awk> </file> <file cate="channel"> <in>PPRMFCHAN</in> <out>channel.txt</out> <awk>chan.awk</awk> </file> <file cate="xcf"> <in>PPRMFXCF</in> <out>xcf.txt</out> <awk>xcf.awk</awk> </file> </files> </IOInfo> <SYSPLEX name="PLEXPP1" CECnumber="4" totalLCPnumber="188" LPARnumber="4"> <LPAR> <name>PP1A</name> <mvsPARName> SP0503</mvsPARName> <LCPNumber>47</LCPNumber> </LPAR> <LPAR> <name>PP1B</name> <mvsPARName> SP0504</mvsPARName> <LCPNumber>47</LCPNumber> </LPAR> <LPAR> <name>PP1C</name> <mvsPARName> SP0505</mvsPARName> <LCPNumber>47</LCPNumber> </LPAR> <LPAR> <name>PP1D</name> <mvsPARName> SP0506</mvsPARName> <LCPNumber>47</LCPNumber> </LPAR> </SYSPLEX> <WLMInfo> <WLMs tp='DB2'> <name>DSNCMSTR</name> <name>DSNCIRLM</name> <name>DSNCDBM1</name> <name>DSNCDIST</name> </WLMs> <WLMs tp='CICS'> <name>CICSAOR1</name> </WLMs> <WLMs tp='Batch'> <name>........</name> </WLMs> </WLMInfo> <timeConfig> <onlineTime>8:00-18:00</onlineTime> <batchTime>18:00-8:00</batchTime> </timeConfig> </root>

  

 

 

测试结果:

 

 

文件类型:sumy 文件输入:PPRMFSUMY 文件输入:sumy.txt awk处理文件:sumy.awk
文件类型:cpu 文件输入:PPRMFCPUALL 文件输入:cpu.txt awk处理文件:cpu.awk
文件类型:wlm 文件输入:PPRMFWLMALL 文件输入:wlm.txt awk处理文件:wlm.awk
文件类型:vstor 文件输入:PPRMFVSTOR 文件输入:vstor.txt awk处理文件:vstor.awk
文件类型:paging 文件输入:PPRMFPAGING 文件输入:paging.txt awk处理文件:page.awk
文件类型:cfSum 文件输入:PPRMFCF1 文件输入:cfSum.txt awk处理文件:cfSum.awk
文件类型:cfSubCh 文件输入:PPRMFCF1 文件输入:cfSubCh.txt awk处理文件:cfSubCh.awk
文件类型:cfStrBa 文件输入:PPRMFCF1 文件输入:cfStrBa.txt awk处理文件:cfStrBa.awk
文件类型:cfStrEx 文件输入:PPRMFCF1 文件输入:cfStrEx.txt awk处理文件:cfStrEx.awk
文件类型:channel 文件输入:PPRMFCHAN 文件输入:channel.txt awk处理文件:chan.awk
文件类型:xcf 文件输入:PPRMFXCF 文件输入:xcf.txt awk处理文件:xcf.awk

 

 


数据库IP:127.0.0.1数据用户名:sa 数据库密码:1234 数据库名称:abcdb 

 

 


==================WLMInfo=========================
DB2
name: DSNCMSTR
name: DSNCIRLM
name: DSNCDBM1
name: DSNCDIST
-------------------
CICS
name: CICSAOR1
-------------------
Batch
name: ........
-------------------
=================WLMInfo==========================

 

你可能感兴趣的:(java,xml,数据库,exception,测试,iterator)