简体   繁体   English

如何为 Python 中的某些标签解析 XML 文件?

[英]How do I parse an XML file for certain tags in Python?

Python: I'm using xml.etree.ElementTree to try and parse an XML file. Python:我正在使用 xml.etree.ElementTree 来尝试解析 XML 文件。 The file looks like this (the ellipses are extra data that is unimportant):该文件如下所示(省略号是不重要的额外数据):

<root xmlns="ns1" xmlns:common="nscommon" ......>
   <item>
      <name> Foo </name>
      .
      .
      .
      <value>
         <min> 0 </min>
         <max> 5 </max>
      </value>
   </item>
   <item>
      <name> Bar </name>
      .
      .
      .
      <value>
         <min> 12 </min>
         <max> 31 </max>
      </value>
   </item>
</root>

My goal is to get the min and max of Foo.我的目标是获得 Foo 的最小值和最大值。 I'm unable to edit the file, so the items cannot have any identifiers.我无法编辑该文件,因此这些项目不能有任何标识符。

I'm using root.iter() to go through the file, so I can get each tag and value.我通过文件使用root.iter()到 go,所以我可以得到每个标签和值。 However, the output would be something like this:但是,output 将是这样的:

Foo
1
2
10
0 <------- goal
5 <------- goal
Bar
Hi
123
9031
12
31

I'm unsure of how to isolate the min and the max specifically, since there will be other numeric data grabbed.我不确定如何具体隔离最小值和最大值,因为会抓取其他数字数据。 Something that says "Hey we're looking at Foo, and all the numbers after Foo but before Bar belong to Foo, and this number is the Min and this is the Max".上面写着“嘿,我们正在查看 Foo,Foo 之后但 Bar 之前的所有数字都属于 Foo,这个数字是 Min,这是 Max”。 This is easier said in English than in code.这在英语中比在代码中更容易说。 Thanks!谢谢!

Here is a general solution with an example reading from an XML string.这是一个通用解决方案,其中包含从 XML 字符串读取的示例。 If you want to get the root from an XML file, you should do instead:如果你想从 XML 文件中获取根,你应该这样做:

tree = ET.parse(file_name)
root = tree.getroot()

Code:代码:

import xml.etree.ElementTree as ET

xml_string = """
<root>
    <item>
        <name> Foo </name>
        <value>
            <min> 0 </min>
            <max> 10 </max>
        </value>
    </item>
</root>
"""

def find_max_min(root, name):
    for item_e in root.iter('item'):
        name_e = item_e.find('name')    
        if name_e.text.strip() == name:
            value_tag = item_e.find('value')
            min_val = int(value_tag.find('min').text)
            max_val = int(value_tag.find('max').text)
            return max_val, min_val

if __name__ == '__main__':
    root = ET.fromstring(xml_string)
    max_val, min_val = find_max_min(root, 'Foo')
    print('max: {}, min: {}'.format(max_val, min_val))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM