how to get direct child nodes not sub-child nodes with same tag name xml minidom python
minidom, python, python-2.7, xml, xml-parsing
Solution
Unless your actual XML is much more complicated, you can navigate the DOM tree and get the child nodes you want from the `text` children in the `parts` nodes that are children of `computer`:
import xml.dom.minidom
file=('sample.xml')
xmldoc=xml.dom.minidom.parse(file)
computerNode = xmldoc.getElementsByTagName('computer')
for computerChild in computerNode:
for parts in computerChild.childNodes:
for partsChild in parts.childNodes:
if partsChild.nodeType == xml.dom.Node.ELEMENT_NODE:
if partsChild.tagName == 'text':
print partsChild.childNodes[0].nodeValue
To use XPath, as I had suggested before, and simpler DOM navigation, its best to use the Element Tree API.
Problem
i have using xml minidom to get some data from xml files but unable to get desire result... try many codes from this site related to xml minidom but fail.. here is my sample xml file.. ``` <computer> <parts> <text>Required</text> </parts> <parts> <text>Required</text> <parts> <text>?Not Required</text> </parts> <parts> <text>?Not Required</text> </parts> </parts> <parts> <text>Required</text> <parts> <text>Not Required</text> </parts> </parts> <parts> <text>Required</text> </parts> </computer> ``` i want to get text that is "required" but get output like that ``` Required Required Not Required Not Required Required Not Required Required ``` here is my code sample that get all text from file but i need text inside such tags that are direct child of parent tag... ``` from xml.dom import minidom file=('d:\sample.xml') xmldoc=minidom.parse(file) parentnode = xmldoc.getElementsByTagName('computer') for node in parentnode: alist=node.getElementsByTagName('text') for a in alist: t=a.childNodes[0].nodeValue print authortext ``` desire output that i want ``` Required Required Required Required ```