Why does etree.tostring () not work for different methods?

I am learning XML and 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 that if I do this, I should get 3 formatted output lines for root: xml, html and text. However, I just get 3 outputs in XML format.

What am I missing here? Did I have to import something? My suspicion is that there is something wrong with the etree.XML destination part, but as I say, I am just following the instructions here. What do people think they are not?

+3


source to share


1 answer


The call results are tostring()

really different, but get lost every time, and you instead print the same expression three times. (Be aware that it tostring()

returns the result without changing its arguments in place .)

If you run this script:

from lxml import etree

root = etree.XML('<html><head/><body><p>Hello<br/>World</p></body></html>')
print(etree.tostring(root, method='xml'))
print(etree.tostring(root, method='html'))
print(etree.tostring(root, method='text'))

      



You will get the expected output:

<html><head/><body><p>Hello<br/>World</p></body></html>
<html><head></head><body><p>Hello<br>World</p></body></html>
HelloWorld

      

+1


source







All Articles