Extracting text from XML node with minidom

后端 未结 3 472
误落风尘
误落风尘 2021-01-19 05:58

I\'ve looked through several posts but I haven\'t quite found any answers that have solved my problem.

Sample XML =




        
3条回答
  •  不思量自难忘°
    2021-01-19 06:35

    A solution with lxml right from the docs:

    from lxml import etree
    from StringIO import StringIO
    
    xml = etree.parse(StringIO('''
    TEXT1TEXT2 TEXT3'''))
    
    xml.xpath("//text()")
    Out[43]: ['\n', 'TEXT1', 'TEXT2 ', 'TEXT3']
    

    You also can extract the text of an specific node:

    xml.find(".//Node[@id='19']").text
    

    The issue here is the text in the XML doesn't belong to any node.

提交回复
热议问题