最近在项目中使用到了在线解析XML,特在此记录一下。
用到的jar包:
commons-configuration-1.9.jar
依赖的包:
commons-collections-3.1.jar
commons-lang-2.6.jar
commons-logging-1.1.2.jar
使用非常简单:
例子是alexa的域名查询回应包:
<ALEXA VER="0.9" URL="baidu.com/" HOME="0" AID="=" IDN="baidu.com/">
<RLS PREFIX="http://" more="82">
<RL HREF="21cn.com/" TITLE="21cn.com"/>
<RL HREF="163.com/" TITLE="网易"/>
<RL HREF="taobao.com/" TITLE="淘宝网"/>
<RL HREF="sohu.com/" TITLE="搜狐首页"/>
<RL HREF="sina.com.cn/" TITLE="新浪首页"/>
<RL HREF="qq.com/" TITLE="QQ.COM"/>
<RL HREF="people.com.cn/" TITLE="人民网"/>
<RL HREF="www.3721.com/" TITLE="网络实名"/>
<RL HREF="search.21cn.com/" TITLE="21cn_com - 搜索引擎"/>
<RL HREF="e.pku.edu.cn/" TITLE="天网中英文搜索"/>
</RLS>
<SD TITLE="A" FLAGS="DMOZ" HOST="baidu.com">
<TITLE TEXT="Baidu.com"/>
<ADDR STREET="The Ideal International Bldg, 12th floor" CITY="Beijing 100080, " STATE="Beijing" ZIP="" COUNTRY=""/>
<CREATED DATE="11-Oct-1999" DAY="11" MONTH="10" YEAR="1999"/>
<PHONE NUMBER="+86 1082607100"/>
<OWNER NAME="2009 Baidu "/>
<EMAIL ADDR="baidu_dns_master@baidu.com"/>
<ALEXAPRO TIER="intro"/>
<LANG LEX="zh-CN"/>
<LINKSIN NUM="304348"/>
<SPEED TEXT="904" PCT="75"/>
<REVIEWS AVG="4.0" NUM="99"/>
<CHILD SRATING="0"/>
</SD>
<KEYWORDS>
<KEYWORD VAL="Chinese Simplified"/>
</KEYWORDS>
<DMOZ>
<SITE BASE="baidu.com/" TITLE="Baidu.com" DESC="The leading Chinese language search engine, provides "simple and reliable" search experience, strong in Chinese language and multi-media content including MP3 music and movies, the first to offer WAP and PDA-based mobile search in China.">
<CATS>
<CAT ID="Top/World/Chinese_Simplified_CN/计算机/互联网络/搜寻/搜索引擎" TITLE="搜寻/搜索引擎" CID="254859"/>
</CATS>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="baidu.com/" TEXT="5" SOURCE="panel"/>
<REACH RANK="6"/>
<RANK DELTA="+0"/>
<COUNTRY CODE="CN" NAME="China" RANK="1"/>
</SD>
</ALEXA>
你想获取百度的反链数,中文排名,全球排名,只需要:
XMLConfiguration prop;
String url = "http://data.alexa.com/data/ezdy01DOo100QI?cli=10&dat=snba&ver=7.0&cdt=alx_vw=20&wid=16865&act=00000000000&ss=1024x768&bw=775&t=0&ttl=1125&vis=1&rq=2&url=www.baidu.com";
prop = new XMLConfiguration(url);
prop.setEncoding("UTF-8");
sinLink = prop.getString("SD.LINKSIN[@NUM]");
worldRank = prop.getString("SD.REACH[@RANK]");
ChinaRank = prop.getString("SD.COUNTRY[@RANK]");
注意getString方法中的参数忽略了根结点ALEXA,直接从其子结点开始即可。