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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With