如何获得直接子节点而不是具有相同标签名称xml minidom python的子子节点

时间:2022-10-27 11:41:35

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..

我已经使用xml minidom从xml文件中获取一些数据但无法获得所需的结果...尝试从这个站点相关的许多代码与xml minidom相关但是失败..这里是我的示例xml文件..

<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

1 个解决方案

#1


2  

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:

除非您的实际XML更复杂,否则您可以导航DOM树并从作为计算机子代的部分节点中的文本子项中获取所需的子节点:

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.

要使用XPath,正如我之前建议的那样,以及更简单的DOM导航,最好使用Element Tree API。

#1


2  

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:

除非您的实际XML更复杂,否则您可以导航DOM树并从作为计算机子代的部分节点中的文本子项中获取所需的子节点:

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.

要使用XPath,正如我之前建议的那样,以及更简单的DOM导航,最好使用Element Tree API。