【问题标题】:How can i parse the following XML script?如何解析以下 XML 脚本?
【发布时间】:2019-06-17 16:00:13
【问题描述】:

我想解析以下 XML 文件。 我已经解析了一些基本的 XML 文件,但这里的问题是它有多个属性(我的意思是多个

<IF caseSensitive="false" field="Placeholder" inputData="PlaceholderiD"...>)

我尝试使用 .findall 函数,但文档说不允许使用引号,因此我真的不知道如何解决我的问题。

我已经尝试用谷歌搜索,但找不到答案。 我是下面的 sn-p 但带引号它不起作用。

 #!/usr/bin/env python3
import xml.etree.ElementTree as ET
tree = ET.parse('SomeFile.xml')
root = tree.getroot()

root.findall("//*[@caseSensitive='"false"']
for child in root:
print (child.tag, child.attrib)

XML 文件:

    <Expressions description="Placeholder" name="Placeholder2">
        <Expression AttributeOne="true" name="JustARandomName">
            <AND AttributeOne="true">
                <OR AttributeOne="true">
                    <IF caseSensitive="false" AttributeTwo="true" field="Placeholder3" AttributeOne="true" inputData="JustAInput1" Operator="true" AnotherOperatorWhichIsNotImportant="false">
                        <SArg dateTime="0">*Something*</SArg>
                    </IF>
                </OR>
                <OR AttributeOne="true">
                    <IF caseSensitive="false" AttributeTwo="false" field="Placeholder4" AttributeOne="true" inputData="JustAInput12" Operator="true" AnotherOperatorWhichIsNotImportant="false">
                        <SArg dateTime="0">Test</SArg>
                    </IF>
                    <AND AttributeOne="true">
                        <IF caseSensitive="false" AttributeTwo="false" field="Placeholder25" AttributeOne="true" inputData="JustAInput13" Operator="true" AnotherOperatorWhichIsNotImportant="false">
                            <SArg dateTime="0">10*</SArg>
                        </IF>
                        <IF caseSensitive="false" AttributeTwo="false" field="Placeholder37" AttributeOne="true" inputData="JustAInput1" Operator="EQUAL" AnotherOperatorWhichIsNotImportant="false">
                            <SArg dateTime="0">true</SArg>
                        </IF>
                        <IF caseSensitive="false" AttributeTwo="true" field="fehlerort" AttributeOne="true" inputData="JustAInput1" Operator="true" AnotherOperatorWhichIsNotImportant="false">
                            <SArg dateTime="0">*Test*</SArg>
                        </IF>
                    </AND> 
...  
... 
...

我尝试使用 &lt;IF case Sensitive="false" ... Operator="true"... &gt; 打印特定行
&lt;IF case Sensitive="false" ... Operator="EQUAL"... &gt; 但不是&lt;IF case Sensitive="false" ... Operator="NOTEQUAL"... &gt; 如果可能的话,只有字段 inputData="..." 但我认为一旦我能够输出整行,我就可以自己解决这个问题。

非常感谢!

【问题讨论】:

    标签: python xml parsing


    【解决方案1】:

    不太确定你打算用它们做什么,但如果你想将它们全部解析到它们自己的列表中,你可以这样做

    例如

    import xml.etree.ElementTree
    
    e = xml.etree.ElementTree.parse('Bx_N63x_Befundverifikation_Komplett.xml').getroot()
    
    listCaseSensitive = []
    listAtt22 = []
    listField = []
    

    不打算把它们都写出来,但你知道了然后你可以做

    for child in e.iter('IF'):
        listCaseSensitive.append(child.attrib['caseSensitive'])
        listAtt2.append(child.attrib['AttributeTwo'])
        listField.append(child.attrib['field'])
    
    print listCaseSensitive
    print listAtt2
    print listField
    

    这将打印出来

    ['false', 'false', 'false', ....] //for Case Sensitive
    ['false','false','false', ...] //for AttributeTwo
    ['Placeholder3','Placeholder4','Placeholder25', .....] //for field
    

    依此类推,我想你有这个想法,所以如果你只想要 InputData,它会以同样的方式工作,你可以从每个 IF 标签中取出 InputData

    如果您正在寻找不同的东西,请告诉我并编辑我的答案

    【讨论】:

      【解决方案2】:

      我尝试使用 .findall 函数,但文档中有引号 不允许

      我认为您在the documentation 语法示例中指的是[@attrib='value']

      该值不能包含引号。

      这只是说该值不能包含引号。 XPath 中仍然需要引号来指示字符串。

      试试……

      for child in root.findall(".//*[@caseSensitive='false']"):
          print(child.tag, child.attrib)
      

      对于您问题的第二部分(忽略Operator="NOTEQUAL"),我认为您不能使用 ElementTree 中的 XPath 来做到这一点。它对 XPath 的支持非常有限。

      不过,您可以使用 XPath in lxml 来做到这一点...

      from lxml import etree
      
      tree = etree.parse("Bx_N63x_Befundverifikation_Komplett.xml")
      
      for input_data in tree.xpath(".//*[@caseSensitive='false'][not(@Operator='NOTEQUAL')]/@inputData"):
          print(input_data)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-10
        • 1970-01-01
        • 2017-04-16
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        • 2012-05-20
        • 2021-12-31
        相关资源
        最近更新 更多