【发布时间】: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>
...
...
...
我尝试使用 <IF case Sensitive="false" ... Operator="true"... > 打印特定行
和<IF case Sensitive="false" ... Operator="EQUAL"... >
但不是<IF case Sensitive="false" ... Operator="NOTEQUAL"... >
如果可能的话,只有字段 inputData="..."
但我认为一旦我能够输出整行,我就可以自己解决这个问题。
非常感谢!
【问题讨论】: