Esempio n. 1
0
from xml.etree.ElementTree import Element,tostring,SubElement,XML
from ElementTree_14 import prettify

top = Element('top')

parent_a = SubElement(top,'parent',id='A')
parent_b = SubElement(top,'parent',id='B')

# Create children
children = XML(
    '<root><child num="0" /><child num="1" /><child num="2" /></root>')

# Set the id to the Python object id of the node
# to make duplicates easier to spot
for c in children:
    c.set('id',str(id(c)))

# Add to first parent
parent_a.extend(children)

print 'A:'
print prettify(top)
print

# Copy nodes to second parent
parent_b.extend(children)

print 'B:'
print prettify(top)
print
Esempio n. 2
0
#! /usr/bin/env/python
# -*- coding:utf-8 -*-

from xml.etree.ElementTree import Element, tostring
from ElementTree_14 import prettify

top = Element('top')
children = [Element('child', num=str(i)) for i in range(3)]
top.extend(children)
print prettify(top)
Esempio n. 3
0
root.append(
    Comment('Generated by ElementTree_csv_to_xml.py for PyMOTW')
    )
head = SubElement(root,'head')
title = SubElement(head,'title')
title.text = 'My Podcasts'
dc = SubElement(head,'dateCreated')
dc.text = generated_on
dm = SubElement(head,'dateModified')
dm.text = generated_on

body = SubElement(root,'body')

with open('podcasts.csv','rt') as f:
    current_group = None
    reader = csv.reader(f)
    for row in reader:
        group_name,podcast_name,xml_url,html_url  = row
        if current_group is None or group_name != current_group.text:
            # Start a new group
            current_group = SubElement(body,'outline',
                                                        {'text':group_name})
        # Add this podcast to the group
        # setting all its attributes at once
        podcast = SubElement(current_group,'outline',
                                           {'text':podcast_name,
                                            'xml_url':xml_url,
                                            'html_url':html_url,
                                            })
print prettify(root)