使用Python解析Xml以查找特定节点值

时间:2015-08-31 14:17:06

标签: python xml lxml elementtree

以下是xml代码

<databases>
            <source>
                    <host>prod</host>
                    <port>1522</port>
                    <user>P11</user>
                    <password>lXXXXX</password>
                    <tns>GP1</tns>

            </source>
            <target>
                    <host>bcp</host>
                    <port>1522</port>
                    <user>pg</user>
                    <password>yyyyy</password>
            </target>
    </databases>

现在想要打印&#34; user&#34;的值和&#34; tns&#34;如果&#34; tns&#34;存在其他打印null。使用Etree lxml但没有得到所需的结果。 感谢。

3 个答案:

答案 0 :(得分:0)

确保你有python-pip包

sudo apt-get install -yq python-pip #for debian-based OSs like ubuntu

从控制台安装xmltodict python包(使用pip)

pip install xmltodict

在python中

import xmltodict

myDict=xmltodict.parse(my_xml_string)
print myDict['databases']['source']['user']

答案 1 :(得分:0)

假设您在databases父节点中嵌套了多个root个标记。将您的内容读入字符串(我使用的是多行Python字符串)

my_string  = '''
<root>
  <databases>
    <source>
        <host>prod</host>
        <port>1522</port>
        <user>P11</user>
        <password>lXXXXX</password>
        <tns>GP1</tns>
    </source>
    <target>
        <host>bcp</host>
        <port>1522</port>
        <user>pg</user>
        <password>yyyyy</password>
    </target>
  </databases>
</root>
'''

然后,您可以通过以下方式获得所需的结果:

from lxml import html

tree = html.fromstring(my_string)
databases = tree.xpath('.//databases')

for database in databases:
    print('User: ' + database.xpath('.//user/text()')[0])
    try: 
        print('TNS: ' + database.xpath('.//tns/text()')[0])
    except:
        print('TNS: null')

答案 2 :(得分:0)

得到结果感谢您的快速回复

 failures = {}
try:
    doc = etree.parse(os.path.join(my_result_dir_name,file_whichI_wanted_to_parse))
    root = doc.getroot()
    for case in root.findall(".//databases"):
        user = case.find("user").text
        tnsTag = case.find("tns")
        if tnsTag is None:
            continue
        failures[user] = (tns.text) if len(errorStackTraceTag.text) > 200 else errorStackTraceTag.text
    return failures
except (Exception,IOError), e:
    return  {
        "error":str(e)
    }