from xml.dom import minidom
from xml.dom.minidom import parse
xmldoc = minidom.parse('sample.xml')
# find the name element, if found return a list, get the first element
name_element = xmldoc.getElementsByTagName("name")[0]
# this will be a text node that contains the actual text
text_node = name_element.childNodes[0]
# get text
print text_node.data
上述公认正确答案的一个小变体是:
这只是使用
nodeValue
而不是它的别名data
请检查一下这个。在
更新
顺便说一句,我建议您ElementTree,下面是使用ElementTree的代码片段,它与上面的minidom代码执行相同的操作
^{pr2}$相关问题 更多 >
编程相关推荐