使用python附加到xml的属性值

时间:2015-10-07 17:33:27

标签: python xml-parsing lxml elementtree

示例xml:

<response version-api="2.0">
  <value>
    <books>
        <book available="20" id="1" tags="">
            <title></title>
            <author id="1" tags="Joel">Manuel De Cervantes</author>
        </book>
        <book available="14" id="2" tags="Jane">
            <title>Catcher in the Rye</title>
           <author id="2" tags="">JD Salinger</author>
       </book>
       <book available="13" id="3" tags="">
           <title></title>
           <author id="3">Lewis Carroll</author>
       </book>
       <book available="5" id="4" tags="Harry">
           <title>Don</title>
           <author id="4">Manuel De Cervantes</author>
       </book>
   </books>
  </value>
</response>

我想将我选择的字符串值附加到名为“tags”的所有属性。这是“tags”属性是否具有值,并且属性是否位于xml结构的不同级别。我已经尝试了findall()方法,但我继续收到错误“IndexError:list index out of range。”这是我到目前为止的代码,虽然有点短,但我已经失去了我需要输入的其他内容......

splitter = etree.XMLParser(strip_cdata=False)
xmldoc = etree.parse(os.path.join(root, xml_file), splitter ).getroot()
for child in xmldoc:
    if child.tag != 'response':
        allDescendants = list(etree.findall())
        for child in allDescendants:
            if hasattr(child, 'tags'):
                child.attribute["tags"].value = "someString"

1 个答案:

答案 0 :(得分:1)

findall()是正确使用的API。这是一个例子:

from lxml import etree
import os

splitter = etree.XMLParser(strip_cdata=False)
xml_file = 'foo.xml'
root = '.'
xmldoc = etree.parse(os.path.join(root, xml_file), splitter ).getroot()
for element in xmldoc.findall(".//*[@tags]"):
    element.attrib["tags"] += " KILROY!"

print etree.tostring(xmldoc)