使用Elelment树包在python中解析XML
实际上,我有以下XML文件:使用Elelment树包在python中解析XML,python,xml,parsing,Python,Xml,Parsing,实际上,我有以下XML文件: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <c:RetriveByVehicleLineModelYearResponse xmlns:a="urn:fo
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<c:RetriveByVehicleLineModelYearResponse xmlns:a="urn:ford/VehicleOrder/LegacyFeatureMapping/v2.0" xmlns:b="urn:ford/VehicleOrder/SingleOrderEdit/v1.0" xmlns:c="urn:ford/interface/VehicleOrder/LegacyFeatureMapping/v2">
<c:PortInstalledOptionFeature>
<a:VehicleLineId>13001</a:VehicleLineId>
<a:ModelYear>2014</a:ModelYear>
<a:LegacyColumn>12</a:LegacyColumn>
<a:LegacyValue>178 </a:LegacyValue>
<a:SalesCode>W78</a:SalesCode>
<a:MappingId>41859</a:MappingId>
<a:MappingSeq>0</a:MappingSeq>
<a:MappingDirection>B</a:MappingDirection>
<a:TargetFeature>
<a:TargetCatgegory>
<a:Id>181</a:Id>
<a:Name>LIGHT TRUCK WHEELBASES </a:Name>
<a:Type>P</a:Type>
<a:FamilyCode>AA5</a:FamilyCode>
</a:TargetCatgegory>
<a:OrderFeatureId>15615</a:OrderFeatureId>
<a:WersCode>AA5K8</a:WersCode>
<a:OrderFeatureName>178 /4521MM WHEELBASE </a:OrderFeatureName>
<a:PIO>false</a:PIO>
<a:SummaryFeature>false</a:SummaryFeature>
</a:TargetFeature>
<a:TargetFeature>
<a:TargetCatgegory>
<a:Id>181</a:Id>
<a:Name>LIGHT TRUCK WHEELBASES </a:Name>
<a:Type>P</a:Type>
<a:FamilyCode>AA5</a:FamilyCode>
</a:TargetCatgegory>
<a:OrderFeatureId>15615</a:OrderFeatureId>
<a:WersCode>AA5K8_second time</a:WersCode>
<a:OrderFeatureName>178 /4521MM WHEELBASE </a:OrderFeatureName>
<a:PIO>false</a:PIO>
<a:SummaryFeature>false</a:SummaryFeature>
</a:TargetFeature>
</c:PortInstalledOptionFeature>
<c:PortInstalledOptionFeature>
<a:VehicleLineId>13001</a:VehicleLineId>
<a:ModelYear>2014</a:ModelYear>
<a:LegacyColumn>12</a:LegacyColumn>
<a:LegacyValue>190 </a:LegacyValue>
<a:SalesCode>W90</a:SalesCode>
<a:MappingId>41860</a:MappingId>
<a:MappingSeq>0</a:MappingSeq>
<a:MappingDirection>B</a:MappingDirection>
<a:TargetFeature>
<a:TargetCatgegory>
<a:Id>181</a:Id>
<a:Name>LIGHT TRUCK WHEELBASES </a:Name>
<a:Type>P</a:Type>
<a:FamilyCode>AA5</a:FamilyCode>
</a:TargetCatgegory>
<a:OrderFeatureId>15616</a:OrderFeatureId>
<a:WersCode>AA5MA</a:WersCode>
<a:OrderFeatureName>190 /4826MM WHEELBASE </a:OrderFeatureName>
<a:PIO>false</a:PIO>
<a:SummaryFeature>false</a:SummaryFeature>
</a:TargetFeature>
</c:PortInstalledOptionFeature>
</c:RetriveByVehicleLineModelYearResponse>
</soapenv:Body>
</soapenv:Envelope>
===============为此,我使用了以下代码:
import glob
import xml.etree.ElementTree as ET
Fatfile = open('#Var_SOE_VLIS_Response_Output\\Sales_to_Wers_Code2.txt', 'a')
try:
tree = ET.parse('#Var_ENG_Response_Files\\SoapResponse1.xml')
Fatfile.write('1111')
WersCodeList = tree.findall('./{urn:ford/VehicleOrder/LegacyFeatureMapping/v2.0}PortInstalledOptionFeature')
Fatfile.write('\n2222')
# x = len(WersCodeList)
# Fatfile.write(x)
Fatfile.write('\n333')
for WersCode in WersCodeList :
Fatfile.write('\n444')
WersCode = WersCode.find('.//{urn:ford/VehicleOrder/LegacyFeatureMapping/v2.0}WersCode')
Fatfile.write('\n')
Fatfile.write(WersCode.text)
except :
Fatfile.write(' \nsorry')
Fatfile.write(' \nSuccess')
====
但我无法使用Findall获取WersCode列表
在这方面请帮忙。。我挣扎了一周先生 尝试使用
lxml
。它比ElementTree
更简单、更快。停止犹豫,读这篇文章到另一个问题。使用搜索词“元素树python findall namespace xml”查找它是很简单的,但是当您的嫌疑犯没有处理名称空间时,您很容易迷失方向
使用适当的名称空间处理,代码也变得非常简单和清晰:
import xml.etree.ElementTree as ET
namespaces = {
"a" : "urn:ford/VehicleOrder/LegacyFeatureMapping/v2.0",
"b" : "urn:ford/VehicleOrder/SingleOrderEdit/v1.0",
"c" : "urn:ford/interface/VehicleOrder/LegacyFeatureMapping/v2"
}
tree = ET.parse('wers.xml')
#print("input file found")
piof_list = tree.findall('.//c:PortInstalledOptionFeature', namespaces)
#print("{0:d} PIOFs found".format(len(piof_list)))
for piof in piof_list:
#print(piof)
wers_code = piof.find('.//a:WersCode', namespaces)
print(wers_code.text)
看看这个:
import xml.etree.ElementTree as ET
namespaces = {
"a" : "urn:ford/VehicleOrder/LegacyFeatureMapping/v2.0",
"b" : "urn:ford/VehicleOrder/SingleOrderEdit/v1.0",
"c" : "urn:ford/interface/VehicleOrder/LegacyFeatureMapping/v2"
}
tree = ET.parse('wers.xml')
#print("input file found")
piof_list = tree.findall('.//c:PortInstalledOptionFeature', namespaces)
#print("{0:d} PIOFs found".format(len(piof_list)))
for piof in piof_list:
#print(piof)
wers_code = piof.find('.//a:WersCode', namespaces)
print(wers_code.text)