查找具有 minidom 属性的元素 [英] Find element with attribute with minidom

查看:21
本文介绍了查找具有 minidom 属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定

<field name="frame.time_delta_displayed" showname="Time delta from previous displayed frame: 0.000008000 seconds" size="0" pos="0" show="0.000008000"/>
<field name="frame.time_relative" showname="Time since reference or first frame: 0.000008000 seconds" size="0" pos="0" show="0.000008000"/>
<field name="frame.number" showname="Frame Number: 2" size="0" pos="0" show="2"/>
<field name="frame.pkt_len" showname="Packet Length: 1506 bytes" hide="yes" size="0" pos="0" show="1506"/>
<field name="frame.len" showname="Frame Length: 1506 bytes" size="0" pos="0" show="1506"/>
<field name="frame.cap_len" showname="Capture Length: 1506 bytes" size="0" pos="0" show="1506"/>
<field name="frame.marked" showname="Frame is marked: False" size="0" pos="0" show="0"/>
<field name="frame.protocols" showname="Protocols in frame: eth:ip:tcp:http:data" size="0" pos="0" show="eth:ip:tcp:http:data"/>

如何在不遍历每个标签并检查属性的情况下立即获取 name="frame.len" 字段?

How do I get the field with name="frame.len" right away without iterating through every tag and checking the attributes?

推荐答案

我不认为你可以.

从父元素,你需要

for subelement in element.GetElementsByTagName("field"):
    if subelement.hasAttribute("frame.len"):
        do_something()

响应您 3 月 11 日的评论,如果您的文档结构稳定且没有令人讨厌的意外(如属性内的尖括号),您可能想要尝试不可思议的并使用正则表达式.这不是推荐的做法,但可以工作并且比实际解析文件要容易得多.我承认我自己有时也这样做过.还没瞎.

Reacting to your comment from March 11, if the structure of your documents is stable and free of nasty surprises (like angle brackets inside attributes), you might want to try the unthinkable and use a regular expression. This is not recommended practice but could work and be much easier than actually parsing the file. I admit that I've done that sometimes myself. Haven't gone blind yet.

因此,在您的情况下,您可以(假设 标签不跨越多行):

So in your case you could (assuming that a <field> tag doesn't span multiple lines):

xmlfile = open("myfile.xml")
for line in xmlfile:
    match = re.search(r'<field\s+name="frame.len"\s+([^>]+)/>', line):
    if match:
        result = match.group(1)
        do_something(result)

如果标签可以跨越多行,您可以尝试将整个文件作为纯文本加载到内存中,然后扫描它以查找匹配项:>

If a <field> tag can span multiple lines, you could try loading the entire file as plain text into memory and then scan it for matches:

filedump = open("myfile.xml").read()
for match in re.finditer(r'<field\s+name="frame.len"\s+([^>]+)/>', filedump):
    result = match.group(1)
    do_something(result)

在这两种情况下,result 将包含除 frame.len 之外的属性.正则表达式假定 frame.len 始终是标签内的第一个属性.

In both cases, result will contain the attributes other than frame.len. The regex assumes that frame.len is always the first attribute inside the tag.

这篇关于查找具有 minidom 属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆