2

I am a newbie to python. I just want to know how i can create a code to write an xml file name data.xml for fusioncharts. The xml file follows the format:

<?xml version="1.0" encoding="UTF-8" ?>
<graph caption="Test graph" xAxisName="X" yAxisName="Y" showAnchors="1" anchorRadius="1" showValues="0">
<set name='2004' value='37800' color='AFD8F8' />
<set name='2005' value='21900' color='F6BD0F' />
<set name='2006' value='32900' color='8BBA00' />
<set name='2007' value='39800' color='FF8E46' />
</graph>

Thanks.

kbb
  • 3,193
  • 3
  • 18
  • 15

3 Answers3

2

You can use the miniDOM library which is built into python 2.0 and later:

http://docs.python.org/library/xml.dom.minidom.html

Andrew Skirrow
  • 3,402
  • 18
  • 41
0

ElementTree from stdlib might be helpful:

import xml.etree.cElementTree as etree

G = etree.Element('graph', dict(caption='Test graph', xAxisName="..."))

for name, value, color in get_sets():
    etree.SubElement(G, 'set', dict(name=name, value=value, color=color))

etree.ElementTree(G).write(open('data.xml', 'wb'), 
                           encoding='utf-8', xml_declaration=True))
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

There's a nice chapter on XML in Dive Into Python 3, most of which also applies to Python 2.x if that's what you're working with. The book/website is an excellent overall resource for a lot of other topics when getting started with Python, too.

Greg Haskins
  • 6,714
  • 2
  • 27
  • 22