使用Python创建XML文档时添加命名空间

时间:2013-10-22 16:43:38

标签: lxml

我需要使用Python创建XML文档,但我无法弄清楚 如何添加

<?xml version="1.0" encoding="utf-8"?>

如何将名称空间元素添加到文档标记

<Document xmlns="urn:iso:std:iso:2013:008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <page1 xmlns="urn:iso:std:iso:2013:008.001.02" </page1>
</Document>

请提供任何示例

1 个答案:

答案 0 :(得分:3)

要输出xml声明<?xml version="1.0" encoding="utf-8"?>,请使用lxml.etree.tostring作为参数。

要添加名称空间元素,请在创建元素时传递xml_declaration=True, encoding='utf-8'参数。

nsmap