我正在使用Python(minidom)来解析XML文件,该文件打印出类似于此的层次结构(此处使用缩进来显示重要的层次关系):
My Document
Overview
Basic Features
About This Software
Platforms Supported
Run Code Online (Sandbox Code Playgroud)
相反,程序在节点上多次迭代并生成以下内容,打印重复节点.(在每次迭代时查看节点列表,很明显为什么它会这样做但我似乎无法找到获取我正在寻找的节点列表的方法.)
My Document
Overview
Basic Features
About This Software
Platforms Supported
Basic Features
About This Software
Platforms Supported
Platforms Supported
Run Code Online (Sandbox Code Playgroud)
这是XML源文件:
<?xml version="1.0" encoding="UTF-8"?>
<DOCMAP>
<Topic Target="ALL">
<Title>My Document</Title>
</Topic>
<Topic Target="ALL">
<Title>Overview</Title>
<Topic Target="ALL">
<Title>Basic Features</Title>
</Topic>
<Topic Target="ALL">
<Title>About This Software</Title>
<Topic Target="ALL">
<Title>Platforms Supported</Title>
</Topic>
</Topic>
</Topic>
</DOCMAP>
Run Code Online (Sandbox Code Playgroud)
这是Python程序:
import xml.dom.minidom
from xml.dom.minidom import Node
dom = xml.dom.minidom.parse("test.xml")
Topic=dom.getElementsByTagName('Topic')
i = 0
for node in Topic: …Run Code Online (Sandbox Code Playgroud)