0

I have lengthy xml this actually ebay listings using ebay api, I am trying to extract following structure in that xml dom:

I am only putting the segment that I am having trouble with, please let me know if you need to see the entire file, I could upload it to a location or do an attachment as a picture.

<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>

Here is dom structure for another ebay item:

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>

But when I tried to extract above elements I endup getting following error:

   attID=att.attributes.getNamedItem('Name').nodeValue
AttributeError: 'NoneType' object has no attribute 'nodeValue'

this is what I get right after I parse response:

[<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>]

And this is what I get inside my for loop before getting the error:

<DOM Element: NameValueList at 0x103398878>

Here is my code:

  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

This is my xml request method:

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
4
  • Is <NameValueList>...</NameValueList> actually in your dataset? Commented Aug 21, 2012 at 18:14
  • yeah the xml dom i have put in the question is actually what get printed out Commented Aug 21, 2012 at 18:28
  • That's interesting. I thought maybe you truncated to but it seemed like a strange place to do so. Commented Aug 21, 2012 at 18:30
  • so I tried a different item and updated the question Commented Aug 21, 2012 at 18:35

1 Answer 1

3

attributes.getNamedItem() gives you the attributes of an element, not it's children, and a <NameValueList> element has no Name attribute, only <Name> elements. You'd have to loop over the contained elements of <NameValueList>, or use .getElementsByTagName('Name') and .getElementsByTagName('Value') to get individual sub-nodes.

Do yourself a big favour though and use the ElementTree API instead; that API is far pythononic and easier to use than the XML DOM API:

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
Sign up to request clarification or add additional context in comments.

11 Comments

thanks for the reply, of this document is not ItemSpecifics> so in that case how will I narrow down based on ElementTreeAPI
@Null-Hypothesis: updated; all Name and Value elements are used to create a results dictionary in that snippet.
@Null-Hypothesis: What is the XML response? Is it a urllib2 urlopen response?
@Null-Hypothesis: data is a python string, so simply use the .fromstring(data) function from the ElementTree module.
@Null-Hypothesis: that's what the link to the documentation is for :-) Use it, there are tutorials there!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.