Adding attributes to existing elements, removing elements, etc with lxml

前端 未结 2 1276
一生所求
一生所求 2021-02-14 13:25

I parse in the XML using

from lxml import etree

tree = etree.parse(\'test.xml\', etree.XMLParser())

Now I want to work on the parsed XML. I\'m

相关标签:
2条回答
  • 2021-02-14 13:59

    You can get to the root element via this call: root=tree.getroot()

    Using that root element, you can use findall() and remove elements that match your criteria:

    deleteThese = root.findall("title")
    for element in deleteThese: root.remove(element)
    

    Finally, you can see what your new tree looks like with this: etree.tostring(root, pretty_print=True)

    Here is some info about how find/findall work: http://infohost.nmt.edu/tcc/help/pubs/pylxml/class-ElementTree.html#ElementTree-find

    To add an attribute to an element, try something like this:

    root.attrib['myNewAttribute']='hello world'
    
    0 讨论(0)
  • 2021-02-14 14:11

    The remove method should do what you want:

    >>> from lxml import etree
    >>> from StringIO import StringIO
    
    >>> s = '<Root><Description><Title>foo</Title></Description></Root>'
    >>> tree = etree.parse(StringIO(s))
    
    >>> print(etree.tostring(tree.getroot()))
    <Root><Description><Title>foo</Title></Description></Root>
    
    >>> title = tree.find('//Title')
    >>> title.getparent().remove(title)
    >>> etree.tostring(tree.getroot())
    '<Root><Description/></Root>'
    
    >>> print(etree.tostring(tree.getroot()))
    <Root><Description/></Root>
    
    0 讨论(0)
提交回复
热议问题