我对 XML 很陌生,我试图从子节点中检索值
from xml.dom import minidom
def Get_ExtList(progName):
progFile='%s.xml'%progName
xmldoc = minidom.parse(progFile)
extList=[]
rootNode=xmldoc.firstChild
progNode=rootNode.childNodes[1]
for fileNodes in progNode.childNodes:
newList=[]
for formatNodes in fileNodes.childNodes:
for nodes in formatNodes.childNodes:
x=nodes.toxml()
x=' '.join(x.split())
newList.append(str(x))
extList.append(newList)
print extList
输出:
[[], [‘.aaa'], [], [‘.bbb'], [], [‘.ccc'], [], [‘.ddd'], [], [‘.xxx', ‘.yyy'], []]
但我想要的东西如下
[[‘.aaa'], [‘.bbb'],[‘.ccc’],[‘.ddd'],[‘.xxx', ‘.yyy']]
这是一个示例文件:
<?xml version="1.0" ?>
<program>
<progname name="TEST">
<file>
<format>
.aaa
</format>
</file>
<file>
<format>
.bbb
</format>
</file>
<file>
<format>
.ccc
</format>
</file>
<file>
<format>
.ddd
</format>
</file>
<file>
<format>
.xxx
</format>
<format>
.yyy
</format>
</file>
</progname>
</program>