Reading XML using Python minidom and iterating over each node

后端 未结 5 1641
既然无缘
既然无缘 2020-12-08 10:38

I have an XML structure that looks like the following, but on a much larger scale:


    
        
             


        
5条回答
  •  悲&欢浪女
    2020-12-08 11:14

    Element nodes don't have a nodeValue. You have to look at the Text nodes inside them. If you know there's always one text node inside you can say element.firstChild.data (data is the same as nodeValue for text nodes).

    Be careful: if there is no text content there will be no child Text nodes and element.firstChild will be null, causing the .data access to fail.

    Quick way to get the content of direct child text nodes:

    text= ''.join(child.data for child in element.childNodes if child.nodeType==child.TEXT_NODE)
    

    In DOM Level 3 Core you get the textContent property you can use to get text from inside an Element recursively, but minidom doesn't support this (some other Python DOM implementations do).

提交回复
热议问题