Python使用untangle模块对XML文件进行解析
untangle库可以将XML文档映射为一个Python 对象,该对象于其结构中包含了原文档的节点与属性信息。该库为第三方库,在使用之前必须使用pip来安装
下面使用的XML示例文件: bookstore.xml
<?xml version="1.0"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
下面是代码及解析: bookstore.py
import untangle
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
doc = untangle.parse("bookstore.xml") # bookstore.xml
print(doc.is_root) # True
print(doc.bookstore.book[0]["category"]) # COOKING
print(doc.bookstore.book[0].author) # Element<author> with attributes{}, children [] and cdata Giada De Laurentiis
print(doc.bookstore.book[0].author.cdata)# Giada De Laurentiis