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
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!
You are on the right track save for a few minor syntax and usage issues:
classis a Python keyword, you can't use it as a function parameter name (which is essentially what
class = 'playlistItem'is doing
data-typeis not a valid variable name in Python, it will be evaluated as
data MINUS type, consider using something like
data_type. There might be ways around this but, IMHO, that would make the code unnecessarily complicated without adding any value
That being said, the following code snippet should give you something usable and you can move on from there. Please feel free to let me know if you need any additional help:
from lxml import etree data_el = etree.Element('data') # You can do this in a loop and keep adding new elements # Note: A deepcopy will be required for subsequent items li_el = etree.SubElement(data_el, "li", class_name = 'playlistItem', data_type = "local", data_mp3 = "PATH") a_el = etree.SubElement(li_el, "a", class_name = 'playlistNotSelected', href='#') print etree.tostring(data_el, encoding='utf-8', xml_declaration = True, pretty_print = True)
This will generate the following output (which you can write to a file):
<?xml version='1.0' encoding='utf-8'?> <data> <li class_name="playlistItem" data_mp3="PATH" data_type="local"> <a class_name="playlistNotSelected" href="#"/> </li> </data>