Next / Previous / Contents / TCC Help System / NM Tech homepage

7.4. The fromstring() function: Create an element from a string

You can create an element or tree of elements from a string containing XML with this function; it returns a new Element instance representing all that XML.

etree.fromstring(s)

where s is a string.

Here's an example:

>>> milne = '''<monster name='Heffalump'>
...   <trail>Woozle</trail>
...   <eeyore mood='boggy'/>
... </monster>'''
>>> doc = etree.fromstring(milne)
>>> print etree.tostring(doc)
<monster name="Heffalump">
  <trail>Woozle</trail>
  <eeyore mood="boggy"/>
</monster>
>>>