如何解析以下XML脚本?

2024-09-30 20:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解析以下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=“…” 但我认为,一旦我能够输出整条线路,我就可以自己解决这个问题

多谢各位


Tags: 文件falsetruefielddatetimeifrootxml
2条回答

I tried to use the .findall function but the Documentation says quotes aren't allowed

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

The value cannot contain quotes.

它只是说值不能包含引号。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)

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

比如说

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

等等,我想你有这个想法,如果你只是想要输入数据,它的工作原理是一样的,你可以从每个if标记中提取输入数据

如果你正在寻找一些不同的东西,请告诉我,我会编辑我的答案

相关问题 更多 >