无法使用Python从XML文件中获取搜索值

时间:2017-06-16 08:50:32

标签: python xml

我需要使用python按照用户输入从XML工作表中搜索值,但是它给出了空白值。我在下面解释我的代码。

import xml.etree.ElementTree as ET
tree = ET.parse('roomlist.xml')
root = tree.getroot()
print(root.findall(".//*[@roomname=\"cottage\"]")) 

我的XML文件如下所示。

<?xml version="1.0" ?><roomlist>
  <location name="Bangalore">
    <room id="1uy92j908u092">
      <roomname> Aquarius </roomname>
      <noseats> 10 </noseats>
      <projectorscreen>yes</projectorscreen>
      <videoconf>yes</videoconf>
    </room>
  </location>
<location name="Bhubaneswar"><room id="131198912460"><roomname>cottage</roomname><noseats>5</noseats><projectorscreen>Yes</projectorscreen><videoconf>Yes</videoconf></room></location><location name="puri"><room id="509955554930"><roomname>room1</roomname><noseats>10</noseats><projectorscreen>No</projectorscreen><videoconf>Yes</videoconf></room></location></roomlist>

这里没有数据。在搜索所有数据之后,我需要将其推入数组中。

2 个答案:

答案 0 :(得分:0)

您可以循环遍历xml中的值,如下所示:

for roomname in root.iter('roomname'):
    print(roomname.text)

所以你可以检查它是否也存在

for roomname in root.iter('roomname'):
    if roomname.text == 'cottage':
        print(roomname.text)

将名称附加到列表中:

lst = []
for roomname in root.iter('roomname'):
    lst.append(roomname.text)

在您的情况下,您需要深入到树中。下面将访问房间节点的属性

for child in root:
    for attr in child.find('room'):
        print(attr)

将输出

<Element 'roomname' at 0x7f9c064d5090>
<Element 'noseats' at 0x7f9c064d50d0>
<Element 'projectorscreen' at 0x7f9c064d5150>
<Element 'videoconf' at 0x7f9c064d5190>
<Element 'roomname' at 0x7f9c064d5250>
<Element 'noseats' at 0x7f9c064d5290>
<Element 'projectorscreen' at 0x7f9c064d52d0>
<Element 'videoconf' at 0x7f9c064d5310>
<Element 'roomname' at 0x7f9c064d53d0>
<Element 'noseats' at 0x7f9c064d5410>
<Element 'projectorscreen' at 0x7f9c064d5450>
<Element 'videoconf' at 0x7f9c064d5490>

您可以使用元素上的.text来创建您需要的词典或列表

另外:https://docs.python.org/3/library/xml.etree.elementtree.html

祝你好运

答案 1 :(得分:0)

您可以在xslt样式表中嵌入python变量,以注入您要查找的房间名称:

from lxml import etree
import sys

if len(sys.argv) > 1:
    roomname = sys.argv[1]

xsl = etree.XML(f'''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="location[room/roomname/text() = \'{roomname}\']">
        <xsl:text>[{{\'lname\':</xsl:text>
        <xsl:value-of select="@name"/>
        <xsl:text>\', </xsl:text>
        <xsl:apply-templates select="room[roomname/text() = \'{roomname}\']/*" />
        <xsl:text>}}]&#xa;</xsl:text>
    </xsl:template>

    <xsl:template match="roomname[text() = \'{roomname}\']">
        <xsl:text>\'rname\':\'</xsl:text>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>\', </xsl:text>
    </xsl:template>

    <xsl:template match="noseats[preceding-sibling::roomname[text() = \'{roomname}\']]">
        <xsl:text>\'noseats\':</xsl:text>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>, </xsl:text>
    </xsl:template>

    <xsl:template match="projectorscreen[preceding-sibling::roomname[text() = \'{roomname}\']]">
        <xsl:text>\'projectorscreen\':\'</xsl:text>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>\', </xsl:text>
    </xsl:template>

    <xsl:template match="videoconf[preceding-sibling::roomname[text() = \'{roomname}\']]">
        <xsl:text>\'videoconf\':\'</xsl:text>
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>\'</xsl:text>
    </xsl:template>

    <xsl:template match="text()" />
</xsl:stylesheet>
''')

transform = etree.XSLT(xsl)
with open("roomlist.xml") as f:
    print(transform(etree.parse(f)), end='')

结果:

$ ./roomlist.py room1
[{'lname':puri', 'rname':'room1', 'noseats':10, 'projectorscreen':'No', 'videoconf':'Yes'}]