Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Python解析XML_Python 3.x_Xml Parsing_Elementtree_Xml.etree - Fatal编程技术网

Python 3.x 使用Python解析XML

Python 3.x 使用Python解析XML,python-3.x,xml-parsing,elementtree,xml.etree,Python 3.x,Xml Parsing,Elementtree,Xml.etree,我有一个需要解析的XML,如下图所示,它在2 elementse.g之间有映射。SAPClient_测试映射到SAPClient <logicalModel id="Join_1"> <descriptions/> <attributes> <attribute id="SAPClient_Test" order="1" attributeHierarchyActiv

我有一个需要解析的XML,如下图所示,它在2 elementse.g之间有映射。SAPClient_测试映射到SAPClient

<logicalModel id="Join_1">
    <descriptions/>
    <attributes>
      <attribute id="SAPClient_Test" order="1" attributeHierarchyActive="false" displayAttribute="false">
        <descriptions defaultDescription="Client"/>
        <keyMapping columnObjectName="Join_1" columnName="SAPClient"/>
      </attribute>
      <attribute id="RegionCode" order="2" attributeHierarchyActive="false" displayAttribute="false">
        <descriptions defaultDescription="First Region"/>
        <keyMapping columnObjectName="Join_1" columnName="RegionCode"/>
      </attribute>
      <attribute id="CountryCode" order="3" attributeHierarchyActive="false" displayAttribute="false">
        <descriptions defaultDescription="Country Code"/>
        <keyMapping columnObjectName="Join_1" columnName="CountryCode"/>
      </attribute>
      <attribute id="CountryName" order="4" attributeHierarchyActive="false" displayAttribute="false">
        <descriptions defaultDescription="Country Name"/>
        <keyMapping columnObjectName="Join_1" columnName="CountryName"/>
      </attribute>
      <attribute id="Nationality" order="5" attributeHierarchyActive="false" displayAttribute="false">
        <descriptions defaultDescription="Nationality"/>
        <keyMapping columnObjectName="Join_1" columnName="Nationality"/>
      </attribute>
    </attributes>
    <calculatedAttributes/>
    <privateDataFoundation>
      <tableProxies/>
      <joins/>
      <layout>
        <shapes/>
      </layout>
    </privateDataFoundation>
    <baseMeasures/>
    <calculatedMeasures/>
    <restrictedMeasures/>
    <localDimensions/>
  </logicalModel>

我建议您将xpath与lxml而不是etree结合使用,使用字典而不是列表,并将整个过程简化一点:

import lxml.html as lh
fly = """[your html above]"""

root = lh.fromstring(fly)

leafs = {}
items = root.xpath('.//attributes[./attribute//keymapping]') #this will ensure keymapping exists
for item in items:
    targets = item.xpath('./attribute/@id') #your desired information is in the attribute value of this and the next nodes
    sources = item.xpath('./attribute/descriptions/@defaultdescription')
    #targets and sources are both lists, so you need to zip through them:
    for target,source in zip(targets,sources):
        leafs[target]=source #add each couple to the dictionary
for target, source in leafs.items():
    print(target, '--->',source)
输出:

SAPClient_Test ---> Client
RegionCode ---> First Region
CountryCode ---> Country Code
CountryName ---> Country Name
Nationality ---> Nationality
SAPClient_Test ---> Client
RegionCode ---> First Region
CountryCode ---> Country Code
CountryName ---> Country Name
Nationality ---> Nationality