我想解析以下XML文件。 我已经解析了一些基本的XML文件,但这里的问题是它有多个属性(我指的是多个属性)
<IF caseSensitive="false" field="Placeholder" inputData="PlaceholderiD"...>)
我试图使用.findall函数,但文档中说不允许使用引号,因此我真的不知道如何解决我的问题
我已经试过用谷歌搜索,但找不到答案。 我看到了下面的代码片段,但加上引号,它就不起作用了
#!/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=“…”
但我认为,一旦我能够输出整条线路,我就可以自己解决这个问题
多谢各位
我想您是在the documentation语法示例中指的
[@attrib='value']
:它只是说值不能包含引号。XPath中仍然需要引号来指示字符串
试试看
对于问题的第二部分(忽略
Operator="NOTEQUAL"
),我认为在ElementTree中使用XPath无法做到这一点。它的XPath支持非常有限不过你可以用XPath in lxml来做
不确定你打算用它们做什么,但如果你想把它们全部解析到它们自己的列表中,你可以这样做
比如说
不打算把它们全部写出来,但你有了想法,然后你就可以做了
这将打印出来
等等,我想你有这个想法,如果你只是想要输入数据,它的工作原理是一样的,你可以从每个if标记中提取输入数据
如果你正在寻找一些不同的东西,请告诉我,我会编辑我的答案
相关问题 更多 >
编程相关推荐