XML 文档中的多个命名空间使用 Python 3 ElementTree
Multiple namespaces in XML doc using Python 3 ElementTree
我正在尝试使用 ElementTree 创建 XML 文档。该文档需要多个命名空间,但我无法在文档或网上找到任何信息来正确执行此操作。我需要顶行有 3 个命名空间,如下所示:
fu01:Page xmlns:xsi="website1" xmlns:fu01="website2" xsi:schemaLocation="website3">
现在我有:
top = ET.Element("fu01:Page")
top.set("xmlns:xsi", "website1")
它打印出第一个命名空间,但我不知道如何将另外两个命名空间也放入其中。
提前致谢!
找到适合我的解决方案。
使用attrib
属性:
top.attrib["hello"] = "hi"
这会将属性添加到元素。
我正在尝试使用 ElementTree 创建 XML 文档。该文档需要多个命名空间,但我无法在文档或网上找到任何信息来正确执行此操作。我需要顶行有 3 个命名空间,如下所示:
fu01:Page xmlns:xsi="website1" xmlns:fu01="website2" xsi:schemaLocation="website3">
现在我有:
top = ET.Element("fu01:Page")
top.set("xmlns:xsi", "website1")
它打印出第一个命名空间,但我不知道如何将另外两个命名空间也放入其中。
提前致谢!
找到适合我的解决方案。
使用attrib
属性:
top.attrib["hello"] = "hi"
这会将属性添加到元素。