我如何解析以下XML脚本?

时间:2019-06-17 16:00:13

标签: python xml parsing

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

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

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

我已经尝试过用Google搜索它,但是找不到答案。 在以下代码段中,但带引号将不起作用。

 #!/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> 
...  
... 
...

我尝试使用<IF case Sensitive="false" ... Operator="true"... >打印特定行
<IF case Sensitive="false" ... Operator="EQUAL"... > 但不是<IF case Sensitive="false" ... Operator="NOTEQUAL"... > 如果可能的话,只需输入inputData =“ ...” 但是我认为一旦我能够输出整条线,我就能自己解决这个问题。

非常感谢您!

2 个答案:

答案 0 :(得分:0)

不太确定您打算如何处理它们,但是如果您想将它们全部解析成自己的列表,则可以这样做

例如

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

如果您正在寻找不同的东西,请告诉我,请修改我的答案

答案 1 :(得分:0)

  

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

我认为您在[@attrib='value']的{​​{3}}语法示例中引用了此内容:

  

该值不能包含引号。

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

尝试...

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

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

尽管您可以用the documentation来做...

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)
相关问题