使用minidom python从父标记xml打印值

使用minidom python从父标记xml打印值,python,xml,minidom,Python,Xml,Minidom,我有一个非常大的xml文件,如果某个标记大于2,我需要知道ID值。 xml文件如下所示: <Users> <Calendar ID="text1"> <Folders>...</Folders> <FolderRights/> <Event/> <EventReminder/> <EventContact/>

我有一个非常大的xml文件,如果某个标记大于2,我需要知道ID值。 xml文件如下所示:

<Users>
    <Calendar ID="text1">
        <Folders>...</Folders>
        <FolderRights/>
        <Event/>
        <EventReminder/>
        <EventContact/>
        <EventRecurrence/>
        <EventException/>
        <ContactItem>
            <COLUMNS>...</COLUMNS>
            <FIELDS>...</FIELDS>
            <FIELDS>...</FIELDS>
            <FIELDS>...</FIELDS>
            <FIELDS>...</FIELDS>
        </ContactItem>
        <ContactLocation>...</ContactLocation>
        <Tags/>
        <TagLinks/>
        <ItemAttr/>
        <ItemAttrData/>
    </Calendar>
    <Calendar ID="text2">
        <Folders>...</Folders>
        <FolderRights/>
        <Event/>
        <EventReminder/>
        <EventContact/>
        <EventRecurrence/>
        <EventException/>
        <ContactItem/>
        <ContactLocation/>
        <Tags/>
        <TagLinks/>
        <ItemAttr/>
        <ItemAttrData/>
    </Calendar>
</Users>
但我没有ID值。
我该怎么做?非常感谢

假设您获得了正确的标记元素,这是访问ID属性的方法:

for contatti in dom.getElementsByTagName('Users'):
    calendars = contatti.getElementsByTagName('Calendar')
    for calendar in calendars:
         attribute = calendar.attributes.get("ID")
         print attribute.name
         print attribute.value

使用lxml非常简单,使用以下方法查找具有>2个contactitem//fields标记的日历父标记:

样本运行:

In [8]: from lxml.html import fromstring

In [9]: tree = fromstring(h)

In [10]: tree.xpath("//calendar[count(./contactitem//fields) > 2]/@id"
   ....: )
Out[10]: ['text1']
或者使用lxml.etree:

from lxml.etree import fromstring

tree = fromstring(h)

print(tree.xpath("//Calendar[count(./ContactItem//FIELDS) > 2]/@ID"))
要从文件中读取,请使用parse:

通常应该从文件中读取,并让lxml处理编码

xml.etree中不支持计数,因此要执行相同的操作,请使用findall:

from xml.etree import ElementTree as et

tree = et.parse("Your.xml")
cals = tree.findall(".//Calendar") 
print([c.get("ID") for c in cals if len(c.findall("./ContactItem/FIELDS")) > 2])

您想得到什么?我想要日历标记上的“text”属性您使用minidom而不是etree或lxml的具体原因是什么?还可以知道我们谈论的是什么文本吗?不,没有特定的原因,所以您希望ContactItem标记有两个以上字段的位置
ID=“text”
?我有一个错误:回溯(最近一次调用):文件“File.py”,第20行,在attribute=calendar.attributes.get(“ID”)中AttributeError:'NodeList'对象没有属性'attributes',好吧,您的xml示例没有任何ContactItem标记,我将更新我的答案,向您展示这是如何与您的示例一起工作的您是否至少尝试过我的代码?例如,StackoverFlow中的要点是教他如何操作,例如教他检查属性值,而不仅仅是复制粘贴而不学习任何内容,ofc他必须添加更多的逻辑。是的,这是可行的,但我必须实现打印,只有当ContactItem中的字段标记有两次或两次以上,但我也可以使用一个文件,而不是用xml文件声明字符串?@tafazzi87m你可以,你应该,只使用parse而不是fromstring,我会在sec@tafazzi87,不用担心,您可以看到,使用lxml使它变得更加简单,并且实际工作如预期的那样
from lxml.etree import fromstring

tree = fromstring(h)

print(tree.xpath("//Calendar[count(./ContactItem//FIELDS) > 2]/@ID"))
from lxml.html import parse
tree = parse("your.xml")
from xml.etree import ElementTree as et

tree = et.parse("Your.xml")
cals = tree.findall(".//Calendar") 
print([c.get("ID") for c in cals if len(c.findall("./ContactItem/FIELDS")) > 2])