How can I create XML files like this?
<?xml version="1.0" encoding="utf-8"?>
<data>
<li class= 'playlistItem' data-type='local' data-mp3='PATH' >
<a class='playlistNonSelected' href='#'>NAME</a>
</li>
...
</data>
I'd create this dynamically and for each item I have, I'd fill in the PATH and NAME variables with the values I need.
I'm trying to use lxml. This is what I've come up with so far, but I don't think it's correct:
from lxml import etree
for item in my_list:
root = etree.Element('li', class = 'playlistItem', data-type = 'local', data-mp3 = PATH)
child = etree.Element('a', class = 'playlistNonSelected', href ='#')
child.text = NAME
Even if the above was correct, I'm lost at this point, because if I have 20 items in the list, how can I do this for each of them and then write it all to an XML file? I've tried looking at other answers but most of the replies are to generate XML like this:
<root>
<child/>
<child>some text</child>
</root>
And I can't figure out how to generate the kind I need. Sorry if I've made obvious mistakes. I appreciate any help. Thank you!