XML解析 dom.minidom

        DOM (  Document Object Model):通过构建一个树结构来表现整个XML文档

一但树被构建,可以通过过DOM提供接口来遍历树和提取相应的数据。

*使用parse() 或   createDocument() 返回为DOM对象

*使用DOM的docuememtElement属性可以获得Root Element

*DOM为树形结构,包含许多nodes,其中element是node的一种,可以包含子elements,textNode也是node的一种,是最终的子节点;

*每个node都有nodeName,nodeValue,nodeType属性,nodeValue是节点的值,只对textNode有效。对于textNode,想要得到他的文本内容,可以使用   .data属性

*getElementsByTagname()可以根据名字来查找子elements

*childNodes返回所有的子Nodes.其中所有的文本均为textNode,包含元素间的“\n\r”和空格 均为 textNode

from xml.dom.minidom import parse
domTree = parse("1.xml")
root= domTree.documentElement #得到根节点列表
book=root.getElementsByTagName("book")#通过标签名字得到得到子节点的地址列表
for b in book:
    bname=b.getElementsByTagName("bname")[0].childNodes[0].data
    price=b.getElementsByTagName("price")[0].childNodes[0].data
    author=b.getElementsByTagName("author")[0].childNodes[0].data
    books.append(Book(bname,price,author))
for i in books:
    print(i)

猜你喜欢

转载自blog.csdn.net/shilaike2/article/details/79447715