Extracting text from XML node with minidom

后端 未结 3 489
误落风尘
误落风尘 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:40

    Using xml.etree.ElemetTree (which is similar to lxml which @DiegoNavrro used in his answer, except that etree in part of the standard library and doesn't have XPATH etc.) you can give the following a go:

    import xml.etree.ElementTree as etree
    
    xml_string = """
    TEXT1TEXT2 TEXT3
    
    """
    
    xml_etree = etree.fromstring(xml_string)
    
    text = [element.tail for element in xml_etree]
    # `text` will be ['TEXT1', 'TEXT2 ', 'TEXT3', '\n']
    

    Note, this assumes that the XML TEXT1... is correct. Because the text follows a closing tag, it becomes the tag's tail text. It is not the elements nodeValue, which is why in your code in the question you are getting Nones.

    If you wanted to parse some XML like TEXT1 you would have to replace the line [element.tail for element in xml_etree] with [element.text for element in xml_etree].

提交回复
热议问题