在ElementTree中使用XPath [英] Using XPath in ElementTree

查看:85
本文介绍了在ElementTree中使用XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML文件如下:

<?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。

All I want to do is extract the ListPrice.

这是我正在使用的代码:

This is the code I am using:

>> 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
>>

完全没有输出。我也尝试过

Absolutely no output. I also tried

>> e = element.findall('Items/Item/ItemAttributes/ListPrice/Amount')

没有区别。

我在做什么错了?

推荐答案

有2个问题

1)元素仅包含根元素,而不递归整个文档。

1) element contains only the root element, not recursively the whole document. It is of type Element not ElementTree.

2)如果将命名空间保留在XML中,则搜索字符串需要使用命名空间。

2) Your search string needs to use namespaces if you keep the namespace in the XML.

要解决问题1:

您需要更改:

element = ET.parse(fp).getroot()

至:

element = ET.parse(fp)

要解决问题2:

您可以起飞xmlns中的xmlns,所以它看起来像这样:

You can take off the xmlns from the XML document so it looks like this:

<?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>

使用此文档,您可以使用以下搜索字符串:

With this document you can use the following search string:

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。

Otherwise you need to specify the xmlns inside the srearch string for each element.

完整代码:

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






两者都打印:


Both print:


2260

2260

这篇关于在ElementTree中使用XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆