在python中使用条件打印相同级别的xml数据

时间:2018-11-19 10:16:51

标签: python xml

我有一个xml文件。我没有制作此文件,所以我不想进行任何更改。我想通过从文件中提取提供某些条件的信息来打印屏幕。我想要获得的值与我将要进行比较的值处于同一水平。因此,即使我可以检查条件,也无法提取其他数据。我该怎么办?

asd.xml

<report>
    <name>asd</name>
    <description>example description - 1</description>
</report>
<report>
    <name>dsa</name>
    <description>example description - 2</description>
</report>
<report>
    <name>asd</name>
    <description>example description - 3</description>
</report>

asd.py

import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()

for report_1 in root:
    for report_2 in report_1:
        if result.tag == 'name':
            if result.text == 'asd':
                print ?

我通过从标记名称“ name”中检索值来找到与asd相等的值。但是我无法访问相同级别的解密值。

我要输出

name: asd
description: example description - 1
name: asd
description: example description - 3

3 个答案:

答案 0 :(得分:1)

您的文件产生错误:

  

ParseError:文档元素后出现垃圾:第5行,第0列

应该像这样:

<data>
    <report>
        <name>asd</name>
        <description>example description - 1</description>
    </report>
    <report>
        <name>dsa</name>
        <description>example description - 2</description>
    </report>
    <report>
        <name>asd</name>
        <description>example description - 3</description>
    </report>
</data>

您可以使用以下代码:

import xml.etree.ElementTree as ET

tree = ET.parse('asd.xml')

root = tree.getroot()

flag = False

for report in root:
    flag = False
    for row in report:
        if row.tag == 'name' and row.text == 'asd':
            print('name: asd')
            flag = True
        if flag and row.tag == 'description':
            print('description: {}'.format(row.text))

这将为您提供所需的结果:

name: asd
description: example description - 1
name: asd
description: example description - 3

答案 1 :(得分:1)

那呢?

asd.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <report>
        <name>asd</name>
        <description>example description - 1</description>
    </report>
    <report>
        <name>dsa</name>
        <description>example description - 2</description>
    </report>
    <report>
        <name>asd</name>
        <description>example description - 3</description>
    </report>
</root>

Python3

import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()

for report in root:
    if report.find('name').text == "asd":
        print("name: ", report.find('name').text)
        print("description: ", report.find('description').text)

答案 2 :(得分:0)

我认为我们可以使用XPath查询来获取正确的输出。

asd.xml

<data>
  <report>
    <name>asd</name>
    <description>example description - 1</description>
  </report>
  <report>
    <name>dsa</name>
    <description>example description - 2</description>
  </report>
  <report>
    <name>asd</name>
    <description>example description - 3</description>
  </report>
</data>

现在,如果我们使用下面提到的此查询,将获得您的预期输出。

'./report[name ="asd"]/description'
import xml.etree.ElementTree as ET


tree = ET.parse('asd.xml')
root = tree.getroot()
print(root)
for key in root.findall('./report[name ="asd"]/description'):
    print(key.tag, key.text)

结果:

<Element 'data' at 0x0000020D4865CEF8>
description example description - 1
description example description - 3