【问题标题】:python lxml - how to get the value of a subelement in XMLpython lxml - 如何获取XML中子元素的值
【发布时间】:2018-02-26 12:20:29
【问题描述】:

XML:

<tree>
  <row>
     <a>This is a</a>
     <b>This is b</b>
  </row>
</tree>

所以我在网上看到了许多解决方案,并且已经查找了其中的许多。以下对我不起作用:

tree = etree.XML('file.xml')
print tree[0].findtext('a'). // None
print tree[0].find('a'). // None
print tree[0].xpath('a') // None
print tree[0].xpath('/a') //None
print tree[0].xpath('//a') //None
print tree[0].xpath('/a') //None
print tree.xpath('//row/a') //None
print tree.xpath('//row/a/text()') //None

我发现的唯一方法就是tree[0][0].text 但是我的实际 XML 包含 25 个子元素,执行 25 次并不是真正干净的代码..

也许你们知道我做错了什么?

我也知道有 BeautifulSoup 之类的东西,但经过测试,我得出的结论是,由于性能,这不适合我的情况.. (benchmark here)

谢谢!

【问题讨论】:

    标签: python xml lxml


    【解决方案1】:

    您可以使用.iterfor 循环。

    for row_node in tree.iter('row'):
        a_node = row_node.find('a')
        b_node = row_node.find('b')
        print(a_node.text)
        print(b_node.text)
    
    # This is a
    # This is b
    

    【讨论】:

      【解决方案2】:

      所以我终于找到了我的问题。这是xml的名称间距。 我没有用它做任何事情,所以我虽然没有必要看。

      XML 略有不同:

      <tree xmlns="http://www.schemas.net/schema/MyXMLSchema">
        <row>
           <a>This is a</a>
           <b>This is b</b>
        </row>
      </tree>
      

      所以我需要在查找中添加命名空间。 为了动态地做到这一点,我使用了另一个 questionanswer 像这样:

      tree = etree.XML('file.xml')
      namespace = tree.xpath('namespace-uri(.)')
      for row in tree:
          print row.findtext('{%s}a' % namespace)
          print row.findtext('{%s}b' % namespace)
      
      # This is a
      # This is b
      

      如果关心的不仅仅是包含行,tree.iter('row') 确实就像DeepSpace 指出的那样,是一个更好的结果。

      【讨论】:

        猜你喜欢
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多