KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm using Python (minidom) to parse an XML file that prints a hierarchical structure that looks something like this (indentation is used here to show the significant hierarchical relationship): My Document Overview Basic Features About This Software Platforms Supported Instead, the program iterates multiple times over the nodes and produces the following, printing duplicate nodes. (Looking at the node list at each iteration, it's obvious why it does this but I can't seem to find a way to get the node list I'm looking for.) My Document Overview Basic Features About This Software Platforms Supported Basic Features About This Software Platforms Supported Platforms Supported Here is the XML source file: <?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> Here is the Python program: 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: alist=node.getElementsByTagName('Title') for a in alist: Title= a.firstChild.data print Title I could fix the problem by not nesting 'Topic' elements, by changing the lower level topic names to something like 'SubTopic1' and 'SubTopic2'. But, I want to take advantage of built-in XML hierarchica
Tags (comma-separated)
Save Edits
Cancel