【python小随笔】python 解析xml数据的新手大坑>>抓取多重标签,遍历各标签的数据

xml文档:
<GetMatchingProductResult ASIN="B071LF9R6G" status="Success">...</GetMatchingProductResult>
<GetMatchingProductResult ASIN="B0714BP3H4" status="Success">...</GetMatchingProductResult>
<GetMatchingProductResult ASIN="B0756FL8R7" status="Success">...</GetMatchingProductResult>
<ResponseMetadata>

# 使用minidom解析器打开XML文档
DOMTree = xml.dom.minidom.parse("./test.xml")
collection = DOMTree.documentElement

# collection是针对整个文件的文档,GetMatchingProductResults 获取的是三个名字相同的标签,不同标签的数据对象
GetMatchingProductResults = collection.getElementsByTagName('GetMatchingProductResult')

# GetMatching 是针对每个标签对象GetMatching作为xml对象进行对应取值
for GetMatching in GetMatchingProductResults:
    asin = GetMatching.getElementsByTagName('ASIN')[0].childNodes[0].data
    print(asin)
原文地址:https://www.cnblogs.com/wanghong1994/p/12179825.html