[英]Python Parse XML using xml.dom.minidom - Extract Items within List
我在使用ebay api的ebay列表中有很長的xml,我試圖在該xml dom中提取以下結構:
我只是將遇到問題的片段放進去,如果您需要查看整個文件,請告訴我,我可以將其上傳到某個位置或做為圖片附件。
<ItemSpecifics>
<NameValueList>
<Name>Room</Name>
<Value>Living Room</Value>
</NameValueList>
<NameValueList>
<Name>Type</Name>
<Value>Sofa Set</Value>
</NameValueList>
<NameValueList>...</NameValueList>
<NameValueList>
<Name>Upholstery Fabric</Name>
<Value>Microfiber</Value>
</NameValueList>
<NameValueList>
<Name>Color</Name>
<Value>Beiges</Value>
</NameValueList>
<NameValueList>
<Name>Style</Name>
<Value>Contemporary</Value>
</NameValueList>
<NameValueList>
<Name>MPN</Name>
<Value>F7615, F7616, F7617, F7618, F7619, F7620</Value>
</NameValueList>
</ItemSpecifics>
這是另一個eBay產品的dom結構:
ItemSpecifics>
<NameValueList>
<Name>Brand</Name>
<Value>Nikon</Value>
</NameValueList>
<NameValueList>
<Name>Model</Name>
<Value>D3100</Value>
</NameValueList>
<NameValueList>
<Name>MPN</Name>
<Value>9798</Value>
</NameValueList>
<NameValueList>
<Name>Type</Name>
<Value>Digital SLR</Value>
</NameValueList>
<NameValueList>
<Name>Megapixels</Name>
<Value>14.2 MP</Value>
</NameValueList>
<NameValueList>
<Name>Optical Zoom</Name>
<Value>3.1x</Value>
</NameValueList>
<NameValueList>
<Name>Screen Size</Name>
<Value>3"</Value>
</NameValueList>
<NameValueList>
<Name>Color</Name>
<Value>Black</Value>
</NameValueList>
</ItemSpecifics>
但是當我嘗試提取上述元素時,我最終遇到以下錯誤:
attID=att.attributes.getNamedItem('Name').nodeValue
AttributeError: 'NoneType' object has no attribute 'nodeValue'
這是我解析響應后得到的結果:
[<DOM Element: NameValueList at 0x103398878>, <DOM Element: NameValueList at 0x103398ab8>, <DOM Element: NameValueList at 0x103398cf8>, <DOM Element: NameValueList at 0x103398f38>, <DOM Element: NameValueList at 0x1033b31b8>, <DOM Element: NameValueList at 0x1033b33f8>, <DOM Element: NameValueList at 0x1033b3638>, <DOM Element: NameValueList at 0x1033b3878>]
這是我在收到錯誤之前進入for循環的內容:
<DOM Element: NameValueList at 0x103398878>
這是我的代碼:
results = {}
attributeSet=response.getElementsByTagName('NameValueList')
print attributeSet
attributes={}
for att in attributeSet:
print att
attID=att.attributes.getNamedItem('Name').nodeValue
attValue=getSingleValue(att,'Value')
attributes[attID]=attValue
result['attributes']=attributes
return result
這是我的xml請求方法:
def sendRequest(apicall,xmlparameters):
connection = httplib.HTTPSConnection(serverUrl)
connection.request("POST", '/ws/api.dll', xmlparameters, getHeaders(apicall))
response = connection.getresponse()
if response.status != 200:
print "Error sending request:" + response.reason
else:
data = response.read()
connection.close()
return data
attributes.getNamedItem()
為您提供元素的屬性,而不是子元素,並且<NameValueList>
元素沒有Name
屬性,只有<Name>
元素。 您必須遍歷<NameValueList>
的包含元素,或使用.getElementsByTagName('Name')
和.getElementsByTagName('Value')
來獲取單個子節點。
不過,請幫自己一個大忙,改用ElementTree API ; 與XML DOM API相比,該API具有很強的Python風格,並且更易於使用:
from xml.etree import ElementTree as ET
etree = ET.fromstring(data)
results = {}
for nvl in etree.findall('NameValueList'):
name = nvl.find('Name').text
value = nvl.find('Value').text
results[name] = value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.