Python 3.x 使用Python解析XML
我有一个需要解析的XML,如下图所示,它在2 elementse.g之间有映射。SAPClient_测试映射到SAPClientPython 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
<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