查找具有 minidom 属性的元素

发布于 2024-08-24 17:43:20 字数 1022 浏览 3 评论 0原文

鉴于

<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" 的字段,而无需迭代每个标签并检查属性?

Given

<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"/>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-08-31 17:43:20

我认为你不能。

从父 元素 中,您需要

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

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

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

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)

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

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 始终是标记内的第一个属性。

I don't think you can.

From the parent element, you need to

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

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)

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.

忆梦 2024-08-31 17:43:20

你不知道 - DOM API,设计得有些糟糕(由 w3c ,不是 Python!-) 没有这样的搜索功能来为您进行迭代。要么接受循环的需要(通常不是通过每个标签,而是通过给定标签名称的所有标签),或者升级到更丰富的接口,例如BeautifulSoup或<代码>lxml。

You don't -- the DOM API, somewhat poorly designed (by w3c, not by Python!-) doesn't have such a search function to do the iteration for you. Either accept the need to loop (not through every tag in general, but through all with a given tag name), or upgrade to a richer interface, such as BeautifulSoup or lxml.

凉月流沐 2024-08-31 17:43:20

哇,那个正则表达式太可怕了!截至 2016 年,每个 DOMElement 都有一个 .getAttribute() 方法,这使事情变得更容易一些,但您仍然必须迭代元素。

l = []
for e in elements:
    if e.hasAttribute('name') and e.getAttribute('name') == 'field.len':
        l.append(e)

Wow, that regex is horrible! As of 2016, there is a .getAttribute() method for each DOMElement that makes things a bit easier, but you still have to iterate through the elements.

l = []
for e in elements:
    if e.hasAttribute('name') and e.getAttribute('name') == 'field.len':
        l.append(e)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文