XML递归搜索并获取属性中特定键的值

时间:2018-03-28 08:25:28

标签: python xml-parsing

这是我的XML文件:

   <?xml version="1.0" encoding="UTF-8"?>

    <edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
      <edmx:Reference Uri="http://docs.oasis-open.org/odata/odata/v4.0/errata03/csd01/complete/vocabularies/Org.OData.Measures.V1.xml">
        <edmx:Include Namespace="Org.OData.Measures.V1" Alias="Measures"/>
      </edmx:Reference>
      <edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/RedfishExtensions_v1.xml">
        <edmx:Include Namespace="RedfishExtensions.v1_0_0" Alias="Redfish"/>
        <edmx:Include Namespace="Validation.v1_0_0" Alias="Validation"/>
      </edmx:Reference>
      <edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/Resource_v1.xml">
        <edmx:Include Namespace="Resource"/>
        <edmx:Include Namespace="Resource.v1_0_0"/>
        <edmx:Include Namespace="Resource.v1_1_0"/>
      </edmx:Reference>
      <edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/Thermal_v1.xml">
        <edmx:Include Namespace="Thermal"/>
      </edmx:Reference>
      <edmx:Reference Uri="http://redfish.dmtf.org/schemas/v1/Power_v1.xml">
        <edmx:Include Namespace="Power"/>
      </edmx:Reference>
    </edmx:Edmx>

我希望使用XML的递归搜索在Uri属性中获取edmx:Reference的值。 我的问题是如何获取XML文件的Uri属性中所有edmx:Reference个键的值?'

2 个答案:

答案 0 :(得分:2)

你可以这样得到你的uris:

var playersNotReady = {...players};

答案 1 :(得分:-1)

使用正则表达式的另一种方法..如果你想使用它:(只是想我也可以包括我的答案)

import re

with open("example.xml") as xml_file:
    xml_data = xml_file.readlines()

    for line in xml_data:
            check = re.findall('<edmx:Reference Uri="(.*)">', line)
            if len(check) != 0:
                print(check[0])