I'm learning XML and am trying the following exercise code:
root = etree.XML('<html><head/><body><p>Hello<br/>World</p></body></html>')
etree.tostring(root, method='xml')
print(etree.tostring(root))
etree.tostring(root, method='html')
print(etree.tostring(root))
etree.tostring(root, method='text')
print(etree.tostring(root))
In the exercise, it says if I do this, I should be getting 3 differently formatted output strings for root: xml, html and text. However, I'm just getting 3 XML-formatted outputs.
What am I missing here? Was I supposed to import something? My suspicion is that something is amiss with the etree.XML assignment part, but as I say: I'm just following directions here. What do people think is amiss?
The results of the
tostring()
calls are indeed different but are lost each time, and you're instead printing the same expression three times. (Be aware thattostring()
is returning a result, not modifying its arguments in place.)If you instead run this script:
You'll get the output you expect: