3

How would I convert an XML element into string and a string back to XML format using xml.elementtree in web2py ?

2 Answers 2

2

Use parseString to get xml element from string and toxml to make string out of xml element. Something like this.

from xml.dom.minidom import parseString

dom = minidom.parseString(content)
...
# do some changes to dom here
return dom.toxml()
Sign up to request clarification or add additional context in comments.

Comments

1

Using the standard library, you'd use the StringIO writer and the parseString function:

>>> from StringIO import StringIO
>>> from xml.dom.minidom import parseString
>>> e = parseString('<foo/>')
>>> out = StringIO()
>>> e.writexml(out)
>>> s = out.getvalue()
>>> print(s)
<?xml version="1.0" ?><foo/>
>>> e2 = parseString(s)

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.