1
>>> stringx
'<?xml version="1.0"?><data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>'
>>>
>>>
>>> e = xml.etree.ElementTree.fromstring(stringx)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xml' is not defined

Can someone suggest what am I missing here? is it the xml code or the way I am trying to parse?

1
  • 2
    Post the full code Commented Jul 3, 2017 at 19:47

3 Answers 3

4

import xml.etree.ElementTree

Looks like you have forgotten this.

Reference the documentation for proper usage.

https://docs.python.org/2/library/xml.etree.elementtree.html

Edit: Thanks @mzjn. Your are correct. I was just passing by and notice OP forgot the import. Didn't actually try it out. Credit goes to you :)

Sign up to request clarification or add additional context in comments.

1 Comment

Just adding import xml won't work. You have to do import xml.etree.ElementTree in order to use xml.etree.ElementTree.fromstring() in the code.
0

python 3

from xml.etree import ElementTree
stringx='<?xml version="1.0"?><data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>'
e = ElementTree.fromstring(stringx)
print(e) # <Element 'data' at 0x7f7e7a4cc1d8>

Comments

-1

seems like you are missing import!

from xml.etree import ElementTree

stringx='<?xml version="1.0"?><data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>'

e = ElementTree.fromstring(stringx) #will wok fine!

Comments

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.