查找具有 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" 的字段,而无需迭代每个标签并检查属性?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你不能。
从父
元素
中,您需要对 3 月 11 日的评论做出反应,如果您的文档结构稳定并且没有令人讨厌的意外情况(例如属性内的尖括号),您可能想尝试不可想象并使用正则表达式。这不是推荐的做法,但可以工作并且比实际解析文件容易得多。我承认我自己有时也这么做过。还没瞎呢
因此,在您的情况下,您可以(假设
标记不跨越多行):如果
标记可以 跨越多行,您可以尝试将整个文件作为纯文本加载到内存中,然后扫描它以查找匹配项:在这两种情况下,
result
将包含除frame.len< 以外的属性/代码>。正则表达式假定
frame.len
始终是标记内的第一个属性。I don't think you can.
From the parent
element
, you need toReacting 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):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:In both cases,
result
will contain the attributes other thanframe.len
. The regex assumes thatframe.len
is always the first attribute inside the tag.你不知道 - 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
orlxml
.哇,那个正则表达式太可怕了!截至 2016 年,每个
DOMElement
都有一个.getAttribute()
方法,这使事情变得更容易一些,但您仍然必须迭代元素。Wow, that regex is horrible! As of 2016, there is a
.getAttribute()
method for eachDOMElement
that makes things a bit easier, but you still have to iterate through the elements.