Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get direct child nodes not sub-child nodes with same tag name xml minidom python

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
like image 317
Ali Malik Avatar asked May 20 '14 12:05

Ali Malik


People also ask

What is XML DOM Minidom?

xml. dom. minidom is a minimal implementation of the Document Object Model interface, with an API similar to that in other languages. It is intended to be simpler than the full DOM and also significantly smaller.

What is childNodes in XML?

This is a read-only property containing a node list of all children for those elements that can have them. The childNodes property is a read-only property containing a node list of all children for those elements that can have them.

Which interface objects Cannot have child nodes in Python?

ProcessingInstruction Objects. Represents a processing instruction in the XML document; this inherits from the Node interface and cannot have child nodes. The content of the processing instruction up to the first whitespace character.


1 Answers

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.

like image 188
helderdarocha Avatar answered Sep 18 '22 21:09

helderdarocha