使用Python将xml声明写入文件

时间:2015-01-09 00:27:50

标签: python xml elementtree

我通过循环遍历字符串列表在python中编写多个xml文件。 假设我有:

from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring

parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
    child.text = ch
    document.write(f, encoding='utf-8', xml_declaration=True)

输出:

<?xml version='1.0' encoding='utf-8'?>
<parent><child>a</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>b</child></parent><?xml version='1.0' encoding='utf-8'?>
<parent><child>c</child></parent>

期望的输出:

<?xml version='1.0' encoding='utf-8'?>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>

我只希望xml声明在文件顶部出现一次。可能应该在循环之前将声明写入文件,但是当我尝试时,我得到空元素。我不想这样做:

f.write('<?xml version='1.0' encoding='utf-8'?>')

如何只将xml声明写入文件?

编辑:所需的输出

3 个答案:

答案 0 :(得分:5)

在编写文件之前,需要将SubItems添加到树中。在您的代码中,您覆盖了相同的元素,并在循环的每次迭代中编写了整个XML文档。

此外,您的XML语法缺少顶级&#34; root&#34;元素有效。

from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring

root = Element('root')

l = ['a', 'b', 'c']
for ch in l:
    parent = SubElement(root,'parent')
    child = SubElement(parent, 'child')
    child.text = ch

document = ElementTree(root)
document.write('file.xml', encoding='utf-8', xml_declaration=True)

,输出结果为:

<?xml version='1.0' encoding='utf-8'?>
<root>
  <parent><child>a</child></parent>
  <parent><child>b</child></parent>
  <parent><child>c</child></parent>
</root> 

答案 1 :(得分:1)

我不熟悉Python的XML库,但让我们退后一步。如果您执行所需操作,则输出将不是有效的XML。 XML必须只有一个root element

所以,你可以:

<?xml version='1.0' encoding='utf-8'?>
<uberparent>
<parent><child>a</child></parent>
<parent><child>b</child></parent>
<parent><child>c</child></parent>
</uberparent>

假设您正在创建Google站点地图,例如:their schema表示根元素是&#34; urlset&#34;。

答案 2 :(得分:0)

只需在xml_declaration上设置 bool()标记,这在技术上是可行的:

parent = Element('parent')
child = SubElement(parent, 'child')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
# use enumerate to have (index, element) pair, started from 0
for i, ch in enumerate(l):
    child.text = ch
    # Start index=0, since bool(0) is Fale, and bool(1..n) is True
    # the flag will be offset
    document.write(f, encoding='utf-8', xml_declaration=bool(not i))
f.close()

更新

由于OP已经意识到所需的输出在语法上是不正确的,并且改变了要求,这里是处理xml的正常方法:

from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring

parent = Element('parent')
f = open('file.xml', 'w')
document = ElementTree(parent)
l = ['a', 'b', 'c']
for ch in l:
    child = SubElement(parent, 'child')
    child.text = ch
document.write(f, encoding='utf-8', xml_declaration=True)
f.close()