简体   繁体   中英

simplest way to generate xml in python

I have a defaultdict(list).. So, data structure of following format:

1:[1,2,3,4,5]
2:[2,3,4]

I want to generate the following xml

<html>
<page>
<src>1</src>
<links>
   <link>1</link>
   <link>2</link>
    ...
    <link>5</link>
</links>
</page>

<page>
<src>2</src>
<links>
   <link>2</link>
   <link>3</link>
    <link>4</link>
</links>
</page>
<html>

And then write an indented xml to file

You can use BeautifulSoup :

from bs4 import Tag


d = {1: [1,2,3,4,5], 2: [2,3,4]}

root = Tag(name='html')
for key, values in d.iteritems():
    page = Tag(name='page')
    src = Tag(name='src')
    src.string = str(key)
    page.append(src)

    links = Tag(name='links')
    for value in values:
        link = Tag(name='link')
        link.string = str(value)
        links.append(link)

    page.append(links)
    root.append(page)

print root.prettify()

prints:

<html>
 <page>
  <src>
   1
  </src>
  <links>
   <link>
    1
   </link>
   <link>
    2
   </link>
   <link>
    3
   </link>
   <link>
    4
   </link>
   <link>
    5
   </link>
  </links>
 </page>
 <page>
  <src>
   2
  </src>
  <links>
   <link>
    2
   </link>
   <link>
    3
   </link>
   <link>
    4
   </link>
  </links>
 </page>
</html>

You can also define a jinja2 template and render it:

from jinja2 import Template


data = {1:[1,2,3,4,5], 2:[2,3,4]}

html = """<html>
    {% for key, values in data.iteritems() %}
        <page>
        <src>{{ key }}</src>
        <links>
            {% for value in values %}
               <link>{{ value }}</link>
            {% endfor %}
        </links>
        </page>
    {% endfor %}
<html>"""

template = Template(html)
print template.render(data=data)

prints:

<html>
        <page>
        <src>1</src>
        <links>
               <link>1</link>
               <link>2</link>
               <link>3</link>
               <link>4</link>
               <link>5</link>
        </links>
        </page>

        <page>
        <src>2</src>
        <links>
               <link>2</link>
               <link>3</link>
               <link>4</link>
        </links>
        </page>
<html>

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