Python XML
XML-Dateien mit Python erstellen
Quelle: http://www.postneo.com/projects/pyxml/--
Beispiel:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# vim: ai ts=4 sts=4 et sw=4
import os
from xml.dom.minidom import Document
class Product(object):
"""Example class"""
def __init__(self, name, price):
self.name = name
self.price = price
if __name__ == '__main__':
#create some products to export in our xml-feed
products = [ Product('house', '100000'), Product('horse', '1000') ]
#create minidom document
doc = Document()
#create base element
entries = doc.createElement('entries')
doc.appendChild(entries)
#create a <product> element for every product
for prod in products:
product = doc.createElement('product')
entries.appendChild(product)
#create a <name> element
name = doc.createElement('name')
#create a <price> element
price = doc.createElement('price')
#append created elements to <product> element
product.appendChild(name)
product.appendChild(price)
#add values to appended elements
name_content = doc.createTextNode(prod.name)
price_content = doc.createTextNode(prod.price)
name.appendChild(name_content)
price.appendChild(price_content)
#save XML to file
datei = open('catalog.xml', 'w')
datei.write(doc.toprettyxml(indent=" "))
datei.close()

