【问题标题】:Parsing Xml using Python to find the certain node values使用 Python 解析 Xml 以查找某些节点值
【发布时间】:2015-08-31 14:17:05
【问题描述】:

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

如果“tns”存在,则现在要打印“user”和“tns”的值,否则打印 null。使用 Etree lxml 尝试但没有得到所需的结果。 谢谢。

【问题讨论】:

  • 您要打印哪些项目?
  • 编辑了如果存在 tns 则要打印 user 和 tns 的问题
  • 我假设您有一个包含多个&lt;database&gt; 标签的大文件?

标签: python xml lxml elementtree


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        相关资源
        最近更新 更多