简体   繁体   中英

Python create XML file

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!

You are on the right track save for a few minor syntax and usage issues:

  1. class is a Python keyword, you can't use it as a function parameter name (which is essentially what class = 'playlistItem' is doing
  2. data-type is not a valid variable name in Python, it will be evaluated as data MINUS type , consider using something like dataType or data_type . There might be ways around this but, IMHO, that would make the code unnecessarily complicated without adding any value ( please see Edit #1 on how to do this )

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>

Edit #0:

Alternatively, you can also write to a file by converting it to an ElementTree first, eg

# Replace sys.stdout with a file object pointing to your object file:
etree.ElementTree(data_el).write(sys.stdout, encoding='utf-8', xml_declaration = True, pretty_print = True)

Edit #1:

Since element attributes are dictionaries, you can use set to specify arbitrary attributes without any restrictions, eg

li_el.set('class', 'playlistItem')
li_el.set('data-type', 'local')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM