使用minidom解析xml--小试牛刀
最近在研究python解析xml。python从来不缺解析xml的库,我分析了一下,综合来看有两个非常合适,一个是大名鼎鼎很低调的xml.dom,一个是强大而且高效的lxml。先来学习minidom的。
这个类实现的readNodes作用是读取节点值和相应的属性
readElementByName是根据输入的元素名字来读取其子元素的结点属性
不是很难理解
先把xml贴上来
得到的结果:
> "D:\Python25\pythonw.exe" -u "D:\学习\python\xml\xml_dom\xml_dom.py"
=========xml_dom==============
policy=====================
prot=====================
other=====================
test_case_id=1
action=====================
res=====================
rule=====================
ID=18612269
value=\x22
=========end===============
>>>>>>>>>xml_dom>>>>>>>>>>
prot>>>>>>>>>>>>>>>>>>>>>>>
dstip>>>>>>>>>>>>>>>>>>>>>>>
dstip:2.2.2.2
dstip>>>>>>>>>>>>>>>>>>>>>>>
dstip:3.3.3.3
dstport>>>>>>>>>>>>>>>>>>>>>>>
dstport:80
srcip>>>>>>>>>>>>>>>>>>>>>>>
srcip:3.3.3.3
srcport>>>>>>>>>>>>>>>>>>>>>>>
srcport:8888
protocol>>>>>>>>>>>>>>>>>>>>>>>
protocol:17
>>>>>>>>>end>>>>>>>>>>>>
=========xml_dom==============
policy=====================
prot=====================
other=====================
test_case_id=1
action=====================
res=====================
rule=====================
ID=18612269
value=\x22
=========end===============
>>>>>>>>>xml_dom>>>>>>>>>>
prot>>>>>>>>>>>>>>>>>>>>>>>
dstip>>>>>>>>>>>>>>>>>>>>>>>
dstip:2.2.2.2
dstip>>>>>>>>>>>>>>>>>>>>>>>
dstip:3.3.3.3
dstport>>>>>>>>>>>>>>>>>>>>>>>
dstport:80
srcip>>>>>>>>>>>>>>>>>>>>>>>
srcip:3.3.3.3
srcport>>>>>>>>>>>>>>>>>>>>>>>
srcport:8888
protocol>>>>>>>>>>>>>>>>>>>>>>>
protocol:17
>>>>>>>>>end>>>>>>>>>>>>
网上从来不缺乏minidom解析的文章,我也是刚学到的。这个还好理解,其实掌握了基本的method就可以应用了。如果想学习更多,可以直接看minidom的源码。
---------------------------------------------------------------------------
低调的python小子
当梦想照进现实 幸福近在咫尺
[jpg]http://ip.ipwind.cn/msn.png[/jpg]

浙公网安备 33010602011771号