0

I have the following XML file which I get from REST API

<?xml version="1.0" encoding="utf-8"?>
<boxes>
  <home id="1" name="productname"/>
  <server>111.111.111.111</server>
  <approved>yes</approved>
  <creation>2007 handmade</creation>
  <description>E-Commerce, buying and selling both attested</description>
  <boxtype>
      <sizes>large, medium, small</sizes>
      <vendor>Some Organization</vendor>
      <version>ANY</version>
  </boxtype>
  <method>Handmade, Handcrafted</method>
  <time>2014</time>
</boxes>

I am able to get the above output, store in a string variable and print in console,

but when I send this to xml ElementTree

import base64
import urllib2
from xml.dom.minidom import Node, Document, parseString
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import XML, fromstring, tostring

print outputxml ##Printing xml correctly,  outputxml contains xml above
content = ET.fromstring(outputxml)
boxes = content.find('boxes')
print boxes
boxtype = boxes.find("boxes/boxtype")

If I print the boxes it is giving me None and hence is giving me below error

boxtype = boxes.find("boxes/boxtype")
AttributeError: 'NoneType' object has no attribute 'find'
3
  • I'm surprised you don't get a parse error -- <method>Handmade, Handcrafted</discoverMethod> is clearly broken! Commented Jan 15, 2015 at 0:04
  • For a start, the line ` <method>Handmade, Handcrafted</discoverMethod>` is invalid. Commented Jan 15, 2015 at 0:05
  • Sorry about that, editing mistake, corrected now. Commented Jan 15, 2015 at 0:06

1 Answer 1

1

The root level node is boxes, and it cannot find boxes within itself.

boxtype = content.find("boxtype")

should be sufficient.

DEMO:

>>> import base64
>>> import urllib2
>>> from xml.dom.minidom import Node, Document, parseString
>>> from xml.etree import ElementTree as ET
>>> from xml.etree.ElementTree import XML, fromstring, tostring
>>> 
>>> print outputxml ##Printing xml correctly,  outputxml contains xml above
<?xml version="1.0" encoding="utf-8"?>
<boxes>
  <home id="1" name="productname"/>
  <server>111.111.111.111</server>
  <approved>yes</approved>
  <creation>2007 handmade</creation>
  <description>E-Commerce, buying and selling both attested</description>
  <boxtype>
      <sizes>large, medium, small</sizes>
      <vendor>Some Organization</vendor>
      <version>ANY</version>
  </boxtype>
  <method>Handmade, Handcrafted</method>
  <time>2014</time>
</boxes>
>>> content = ET.fromstring(outputxml)
>>> boxes = content.find('boxes')
>>> print boxes
None
>>> 
>>> boxes
>>> content #note that the content is the root level node - boxes
<Element 'boxes' at 0x1075a9250> 
>>> content.find('boxtype')
<Element 'boxtype' at 0x1075a93d0>
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

sure, either ways, the issue is - as in the answer.. the content is the boxes element you are looking for.

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.