Change text value with lxml

后端 未结 1 1885
长情又很酷
长情又很酷 2020-12-30 10:22

I have an xml file - here is a snippet..

  
    {0328cb65-b564-495a-b17e-e49e04864ab7}

        
相关标签:
1条回答
  • 2020-12-30 10:39

    You'll have to query the whole node instead of only its contents:

    code = root.xpath('//gmd_identifier/gmd_RS_Identifier/gmd_code/gco_CharacterString')
    

    Then, if it matched, just replace its text and save back to the XML file:

    if code:
        # Replaces <gco_CharacterString> text
        code[0].text = '{0328cb65-b564-495a-b17e-e49e04864ab7}'
        # Save back to the XML file
        etree.ElementTree(root).write('D:\test.xml', pretty_print=True)
    

    That's all! :)

    0 讨论(0)
提交回复
热议问题