我的 XML文件如下所示: ?xml version="1.0"?ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2008-08-19" Items Item ItemAttributes ListPrice Amount2260/Amount /ListPrice /ItemAttributes Offers Offer OfferLi
<?xml version="1.0"?> <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2008-08-19"> <Items> <Item> <ItemAttributes> <ListPrice> <Amount>2260</Amount> </ListPrice> </ItemAttributes> <Offers> <Offer> <OfferListing> <Price> <Amount>1853</Amount> </Price> </OfferListing> </Offer> </Offers> </Item> </Items> </ItemSearchResponse>
我想做的就是提取ListPrice.
这是我正在使用的代码:
>> from elementtree import ElementTree as ET >> fp = open("output.xml","r") >> element = ET.parse(fp).getroot() >> e = element.findall('ItemSearchResponse/Items/Item/ItemAttributes/ListPrice/Amount') >> for i in e: >> print i.text >> >> e >>
绝对没有输出.我也试过了
>> e = element.findall('Items/Item/ItemAttributes/ListPrice/Amount')
没有不同.
我究竟做错了什么?
你有两个问题.1)元素只包含根元素,而不是递归地包含整个文档.它的类型为Element而不是ElementTree.
2)如果将命名空间保留在XML中,则搜索字符串需要使用命名空间.
解决问题#1:
你需要改变:
element = ET.parse(fp).getroot()
至:
element = ET.parse(fp)
解决问题#2:
您可以从XML文档中取出xmlns,使其如下所示:
<?xml version="1.0"?> <ItemSearchResponse> <Items> <Item> <ItemAttributes> <ListPrice> <Amount>2260</Amount> </ListPrice> </ItemAttributes> <Offers> <Offer> <OfferListing> <Price> <Amount>1853</Amount> </Price> </OfferListing> </Offer> </Offers> </Item> </Items> </ItemSearchResponse>
使用此文档,您可以使用以下搜索字符串:
e = element.findall('Items/Item/ItemAttributes/ListPrice/Amount')
完整代码:
from elementtree import ElementTree as ET fp = open("output.xml","r") element = ET.parse(fp) e = element.findall('Items/Item/ItemAttributes/ListPrice/Amount') for i in e: print i.text
对问题#2的替代解决方法:
否则,您需要在srearch字符串中为每个元素指定xmlns.
完整代码:
from elementtree import ElementTree as ET fp = open("output.xml","r") element = ET.parse(fp) namespace = "{http://webservices.amazon.com/AWSECommerceService/2008-08-19}" e = element.findall('{0}Items/{0}Item/{0}ItemAttributes/{0}ListPrice/{0}Amount'.format(namespace)) for i in e: print i.text
两个印刷品:
2260