繁体   English   中英

使用xml.etree.ElementTree在任何嵌套深度进行通配符搜索

[英]Wildcard search at any nested depth using xml.etree.ElementTree

我有一组XML文件,其中包含类似

   <group name="XXX common string">

      <value val="12" description="a dozen">
         <text>one less than a baker's dozen</text>
      </value>

      <value val="13" description="a baker's dozen">
         <text>One more than a dozen</text>
      </value>

   </group>

   <group name="YYY common string">

      <value val="42" description="the answer">
         <text>What do you get if you multiple 6 by 9?</text>
      </value>

   </group>

有什么简单的方法,使用import xml.etree.ElementTree as ET

    parser = ET.XMLParser()
    parser.parser.UseForeignDTD(True)

    if (args.info) or (args.diagnostics):
        print('Parsing input file : ' + inputFileName)

    tree = ET.parse(inputFileName, parser=parser)
    root = tree.getroot()

仅搜索名称包含“ common string”特定value val <group>元素?

重要说明:这些组嵌套在不同文件中的不同深度。

这有点困难,因为您自己的代码无法与您在问题中发布的示例数据一起使用(例如,没有任何内容包含字符串error ,并且没有id属性,并且您的代码似乎无法搜索“特定value val ,这似乎是您的要求之一)。但是这里有一些建议...

为了找到所有在name属性中包含common string group元素,可以执行以下操作:

>>> matching_groups = []
>>> for group in tree.xpath('//group[contains(@name, "common string")]'):
...   matching_groups.append[group]
...

给定您的样本数据将导致:

>>> print '\n'.join([etree.tostring(x) for x in matching_groups])
<group name="XXX common string">

      <value val="12" description="a dozen">
         <text>one less than a baker's dozen</text>
      </value>

      <value val="13" description="a baker's dozen">
         <text>One more than a dozen</text>
      </value>

   </group>


<group name="YYY common string">

      <value val="42" description="the answer">
         <text>What do you get if you multiple 6 by 9?</text>
      </value>

   </group>

如果您希望将结果限制为仅包含属性value val == 42 value元素的group元素,则可以尝试:

>>> matching_groups = []
>>> for group in tree.xpath('//group[contains(@name, "common string")][value/@val = "42"]'):
...     matching_groups.append(group)
... 

这将产生:

>>> print '\n'.join([etree.tostring(x) for x in matching_groups])
<group name="YYY common string">

      <value val="42" description="the answer">
         <text>What do you get if you multiple 6 by 9?</text>
      </value>

   </group>

问题是:1)通配符搜索组名,以及2)组在不同文件中嵌套在不同级别的事实。

我实现了这种蛮力方法,以在文件中任何位置的名为group的错误中构建所有此类错误条目的字典。

我将其留在此处供后代使用,并邀请更多的大象来解决。

    import xml.etree.ElementTree as ET

    parser = ET.XMLParser()
    parser.parser.UseForeignDTD(True)

    tree = ET.parse(inputFileName, parser=parser)
    root = tree.getroot()

    args.errorDefinitions = {}
    for element in tree.iter():
        if element.tag == 'group':
            if 'error' in element.get('name').lower():
                if element._children:
                    for errorMessage in element._children[0]._children:
                        args.errorDefinitions[errorMessage.get('name')] = \
                                  {'id':  errorMessage.get('id'), \
                                  'description': element._children[0].text}

暂无
暂无

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

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