Why is BeautifulSoup modifying my self-closing elements?

This is the script I have:

import BeautifulSoup if __name__ == "__main__": data = """ <root> <obj id="3"/> <obj id="5"/> <obj id="3"/> </root> """ soup = BeautifulSoup.BeautifulStoneSoup(data) print soup 

At startup, this prints:

 <root> <obj id="3"></obj> <obj id="5"></obj> <obj id="3"></obj> </root> 

I would like to keep the same structure. How can i do this?

+2
python xml beautifulsoup
source share
1 answer

From the Beautiful Soup Documentation :

The most common drawback of BeautifulStoneSoup is that it does not know about self-closing tags. HTML has a fixed set of self-closing tags, but with XML it depends on what the DTD says. You can tell BeautifulStoneSoup that certain tags are self-closing by passing their names as an argument to selfClosingTags constructor

+7
source share

All Articles