我有这个XML文件:
<?xml version="1.0" ?><XMLSchemaPalletLoadTechData xmlns="http://tempuri.org/XMLSchemaPalletLoadTechData.xsd">
<TechDataParams>
<RunNumber>sample</RunNumber>
<Holder>sample</Holder>
<ProcessToolName>sample</ProcessToolName>
<RecipeName>sample</RecipeName>
<PalletName>sample</PalletName>
<PalletPosition>sample</PalletPosition>
<IsControl>sample</IsControl>
<LoadPosition>sample</LoadPosition>
<HolderJob>sample</HolderJob>
<IsSPC>sample</IsSPC>
<MeasurementType>sample</MeasurementType>
</TechDataParams>
<TechDataParams>
<RunNumber>sample</RunNumber>
<Holder>sample</Holder>
<ProcessToolName>sample</ProcessToolName>
<RecipeName>sample</RecipeName>
<PalletName>sample</PalletName>
<PalletPosition>sample</PalletPosition>
<IsControl>sample</IsControl>
<LoadPosition>sample</LoadPosition>
<HolderJob>sample</HolderJob>
<IsSPC>sample</IsSPC>
<MeasurementType>XRF</MeasurementType>
</TechDataParams>
</XMLSchemaPalletLoadTechData>
这是我解析xml的代码:
for data in xml.getElementsByTagName('TechDataParams'):
#parse xml
runnum=data.getElementsByTagName('RunNumber')[0].firstChild.nodeValue
hold=data.getElementsByTagName('Holder')[0].firstChild.nodeValue
processtn=data.getElementsByTagName('ProcessToolName'[0].firstChild.nodeValue)
recipedata=data.getElementsByTagName('RecipeName'[0].firstChild.nodeValue)
palletna=data.getElementsByTagName('PalletName')[0].firstChild.nodeValue
palletposi=data.getElementsByTagName('PalletPosition')[0].firstChild.nodeValue
control = data.getElementsByTagName('IsControl')[0].firstChild.nodeValue
loadpos=data.getElementsByTagName('LoadPosition')[0].firstChild.nodeValue
holderjob=data.getElementsByTagName('HolderJob')[0].firstChild.nodeValue
spc = data.getElementsByTagName('IsSPC')[0].firstChild.nodeValue
mestype = data.getElementsByTagName('MeasurementType')[0].firstChild.nodeValue
但是当我打印每个节点时,我只能得到一组“ TechDataParams”,但是我希望能够从XML中获得所有的“ TechDataParams”。
让我知道我的问题是否还不清楚。
请勿使用解析XML minidom
,除非您希望自己拔掉头发。
我会在这里使用xmltodict
模块。一行,就可以找到一列包含所有所需数据的字典:
import xmltodict
data = """your xml here"""
data = xmltodict.parse(data)['XMLSchemaPalletLoadTechData']['TechDataParams']
for params in data:
print dict(params)
印刷:
{u'PalletPosition': u'sample', u'HolderJob': u'sample', u'RunNumber': u'sample', u'ProcessToolName': u'sample', u'RecipeName': u'sample', u'IsControl': u'sample', u'PalletName': u'sample', u'LoadPosition': u'sample', u'MeasurementType': u'sample', u'Holder': u'sample', u'IsSPC': u'sample'}
{u'PalletPosition': u'sample', u'HolderJob': u'sample', u'RunNumber': u'sample', u'ProcessToolName': u'sample', u'RecipeName': u'sample', u'IsControl': u'sample', u'PalletName': u'sample', u'LoadPosition': u'sample', u'MeasurementType': u'XRF', u'Holder': u'sample', u'IsSPC': u'sample'}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句