I am trying to convert structure like this (some nested xml/html)
<div>a comment
<div>an answer</div>
<div>an answer
<div>a reply</div>
...
</div>
...
</div>
...
clarification: it can be formatted like <div>a comment><div>an answer</div> or in any other way (not prettified etc)
(which has multiple nodes of different depth)
to corresponding list structure which has parent <ul> tags (i.e. ordinary html list)
<ul>
<li>1
<ul>
<li>2</li>
...
</ul>
</li>
...
</ul>
I tried to use BeautifulSoup like this:
from bs4 import BeautifulSoup as BS
bs = BS(source_xml)
for i in bs.find_all('div'):
i.name = 'i'
# but it only replaces div tags to li tags, I still need to add ul tags
I can iterate through indentation levels like this, but I still can't figure how to separate a group of tags located on the same level to add the ul tag to them:
for i in bs.find_all('div', recursive=False):
# how to wrap the following iterated items in 'ul' tag?
for j in i.find_all('div', recursive=False):
...
how can one add <ul> tags in right places? (I don't care about pretty printing etc, I need valid html structure with ul and li tags, tnx...)