在python和lxml中生成xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4469983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:52:55  来源:igfitidea点击:

Generating xml in python and lxml

pythonxmllxml

提问by user278618

I have this xml from sql, and I want to do the same by python 2.7 and lxml

我有来自 sql 的这个 xml,我想用 python 2.7 和 lxml 做同样的事情

<?xml version="1.0" encoding="utf-16"?>
<results>
  <Country name="Germany" Code="DE" Storage="Basic" Status="Fresh" Type="Photo" />
</results>

Now I have:

我现在有:

from lxml import etree

# create XML 
results= etree.Element('results')

country= etree.Element('country')
country.text = 'Germany'
root.append(country)



filename = "xmltestthing.xml"
FILE = open(filename,"w")
FILE.writelines(etree.tostring(root, pretty_print=True))
FILE.close()

Do you know how to add rest of attributes?

你知道如何添加其余的属性吗?

采纳答案by Marco Mariani

Note this also prints the BOM

请注意,这也会打印 BOM

>>> from lxml.etree import tostring
>>> from lxml.builder import E
>>> print tostring(
             E.results(
                 E.Country(name='Germany',
                           Code='DE',
                           Storage='Basic',
                           Status='Fresh',
                           Type='Photo')
             ), pretty_print=True, xml_declaration=True, encoding='UTF-16')

??<?xml version='1.0' encoding='UTF-16'?>
<results>
  <Country Status="Fresh" Type="Photo" Code="DE" Storage="Basic" name="Germany"/>
</results>

回答by user225312

from lxml import etree

# Create the root element
page = etree.Element('results')

# Make a new document tree
doc = etree.ElementTree(page)

# Add the subelements
pageElement = etree.SubElement(page, 'Country', 
                                      name='Germany',
                                      Code='DE',
                                      Storage='Basic')
# For multiple multiple attributes, use as shown above

# Save to XML file
outFile = open('output.xml', 'w')
doc.write(outFile, xml_declaration=True, encoding='utf-16') 

回答by John Machin

Promoting my comment to an answer:

将我的评论提升为答案:

@sukbir is probably not using Windows. What happens is that lxml writes a newline (0A 00 in UTF-16LE) between the XML header and the body. This is then molested by Win text mode to become 0D 0A 00 which makes everything after that look like UTF-16BE hence the Chinese etc characters when you display it. You can get around this in this instance by using "wb" instead of "w" when you open the file. However I'd strongly suggest that you use 'UTF-8' (spelled EXACTLY like that) as your encoding. Why are you using UTF-16? You like large files and/or weird problems?

@sukbir 可能没有使用 Windows。发生的情况是 lxml 在 XML 标头和正文之间写入换行符(UTF-16LE 中的 0A 00)。然后它被 Win 文本模式骚扰为 0D 0A 00,这使得之后的所有内容看起来都像 UTF-16BE,因此显示时会出现中文等字符。在这种情况下,您可以通过在打开文件时使用“wb”而不是“w”来解决此问题。但是,我强烈建议您使用“UTF-8”(完全像那样拼写)作为编码。你为什么使用UTF-16?你喜欢大文件和/或奇怪的问题吗?

回答by Habib

Save to XML file

保存到 XML 文件

doc.write('output.xml', xml_declaration=True, encoding='utf-16') 

instead of:

代替:

outFile = open('output.xml', 'w')

doc.write(outFile, xml_declaration=True, encoding='utf-16')