2

我是python的新手。我只想知道如何创建代码来为 fusioncharts 编写 xml 文件名 data.xml。xml 文件遵循以下格式:

<?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>

谢谢。

4

3 回答 3

2

您可以使用内置于 python 2.0 及更高版本的 miniDOM 库:

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

于 2011-03-28T18:22:40.663 回答
0

来自 stdlib 的ElementTree可能会有所帮助:

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))
于 2011-03-28T20:53:56.667 回答
0

Dive Into Python 3中有一个关于 XML 的精彩章节,如果您正在使用它,其中大部分也适用于 Python 2.x。在开始使用 Python 时,这本书/网站也是许多其他主题的优秀整体资源。

于 2011-03-28T21:04:13.400 回答