如何使用Python的cElementTree创建

时间:2012-01-15 07:38:08

标签: python xml elementtree

我试图在这个问题中使用答案,但无法使其有效:How to create "virtual root" with Python's ElementTree?

这是我的代码:

import xml.etree.cElementTree as ElementTree
from StringIO import StringIO
s = '<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" ><tmx version=\"1.4a\" />'
tree = ElementTree.parse(StringIO(s)).getroot()
header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',})
body = ElementTree.SubElement(tree,'body')
ElementTree.ElementTree(tree).write('myfile.tmx','UTF-8')

当我打开生成的'myfile.tmx'文件时,它包含:

<?xml version='1.0' encoding='UTF-8'?>
<tmx version="1.4a"><header adminlang="EN" /><body /></tmx>

我错过了什么?或者,有更好的工具吗?

3 个答案:

答案 0 :(得分:11)

您可以将write函数的xml_declaration参数设置为False,因此输出不会包含带编码的xml声明,然后只需手动附加所需的标题。实际上,如果您将编码设置为'utf-8'(小写),则也不会添加xml声明。

import xml.etree.cElementTree as ElementTree

tree = ElementTree.Element('tmx', {'version': '1.4a'})
ElementTree.SubElement(tree, 'header', {'adminlang': 'EN'})
ElementTree.SubElement(tree, 'body')

with open('myfile.tmx', 'wb') as f:
    f.write('<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE tmx SYSTEM "tmx14a.dtd">'.encode('utf8'))
    ElementTree.ElementTree(tree).write(f, 'utf-8')

生成的文件(为了便于阅读而手动添加新行):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tmx SYSTEM "tmx14a.dtd">
<tmx version="1.4a">
    <header adminlang="EN" />
    <body />
</tmx>

答案 1 :(得分:9)

您可以使用lxml及其tostring功能:

   
from lxml import etree

s = """<?xml version="1.0" encoding="UTF-8"?>
<tmx version="1.4a"/>""" 

tree = etree.fromstring(s)
header = etree.SubElement(tree,'header',{'adminlang': 'EN'})
body = etree.SubElement(tree,'body')

print etree.tostring(tree, encoding="UTF-8",
                     xml_declaration=True,
                     pretty_print=True,
                     doctype='<!DOCTYPE tmx SYSTEM "tmx14a.dtd">')

=&GT;

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE tmx SYSTEM "tmx14a.dtd">
<tmx version="1.4a">
  <header adminlang="EN"/>
  <body/>
</tmx>

答案 2 :(得分:1)

我使用了不同的解决方案来添加DOCTYPE,非常简单,非常愚蠢。

$user = $graph->createRequest("GET", "/users")
              ->setReturnType(Model\User::class)
              ->execute();