使用 Python 解析 Xml 以查找某些节点值

Parsing Xml using Python to find the certain node values

下面是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>

现在想要打印 "user" 和 "tns" 的值,如果 "tns" 存在,否则使用 E​​tree lxml 打印 null.Tried 但没有得到想要的结果。 谢谢

确保你有 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']

假设您有多个 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')

得到结果感谢您的及时回复

 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)
    }