使用 xml.etree.ElementTree 添加节点到 XML 时如何添加命名空间?

问题描述 投票:0回答:2

我有下一个代码来解析一个 XML 字符串,然后向 XML 添加一个新节点。但你可以看到我的代码只能添加

<category term="Platform Version" value="Linux_6.6" />
而没有命名空间。 我需要在
ns0
之前添加
category

test_xml.py

import xml.etree.ElementTree as ET

source_str = """<?xml version="1.0" encoding="UTF-8"?>
<ns0:testcase xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/">
<ns0:category term="Platform Version" value="Linux_5.4"/>
</ns0:testcase>
"""

xml_root = ET.fromstring(source_str)
new_str = '<category term="Platform Version" value="Linux_6.6"/>'
new_node = ET.fromstring(new_str)
xml_root.append(new_node)
final_str = ET.tostring(xml_root, encoding='utf-8').decode('utf-8')

print(final_str)

执行:

$ python3 test_xml.py
<ns0:testcase xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/">
<ns0:category term="Platform Version" value="Linux_5.4" />
<category term="Platform Version" value="Linux_6.6" /></ns0:testcase>

我的期望:

<ns0:testcase xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/">
<ns0:category term="Platform Version" value="Linux_5.4" />
<ns0:category term="Platform Version" value="Linux_6.6" /></ns0:testcase>

如果我像这样更改代码:

new_str = '<ns0:category term="Platform Version" value="Linux_6.6"/>'
new_node = ET.fromstring(new_str)

它只会给我错误:

Traceback (most recent call last):
  File "/home/nxa13855/test_xml.py", line 11, in <module>
    new_node = ET.fromstring(new_str)
  File "/usr/lib/python3.9/xml/etree/ElementTree.py", line 1347, in XML
    parser.feed(text)
xml.etree.ElementTree.ParseError: unbound prefix: line 1, column 0

我如何管理我的目标?

python xml elementtree
2个回答
0
投票

我通过在定义新节点字符串时添加

namespace
和定义
xmlns
来管理它:

new_str = '<ns0:category term="Platform Version" value="Linux_6.6" xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/"/>'

这样,代码就不会再打印错误了,因为它找到了

ns0
的前缀映射。

执行:

$ python3 test_xml.py
<ns0:testcase xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/">
<ns0:category term="Platform Version" value="Linux_5.4" />
<ns0:category term="Platform Version" value="Linux_6.6" /></ns0:testcase>

0
投票

您可以使用

register_namespace(prefix, uri)
。前缀
ns0
只是占位符。您应该使用 qm,请参阅 IBM

import xml.etree.ElementTree as ET

source_str = """<?xml version="1.0" encoding="UTF-8"?>
<ns0:testcase xmlns:ns0="http://jazz.net/xmlns/alm/qm/v0.1/">
<ns0:category term="Platform Version" value="Linux_5.4"/>
</ns0:testcase>
"""

xml_root = ET.fromstring(source_str)

ET.register_namespace("qm", "http://jazz.net/xmlns/alm/qm/v0.1/")

new_str = '<category term="Platform Version" value="Linux_6.6"/>'
new_node = ET.fromstring(new_str)
xml_root.append(new_node)

# Pretty Print and write to file
tree = ET.ElementTree(xml_root)
ET.indent(tree, space="  ")
tree.write("test.xml", xml_declaration=True, encoding="utf-8")

ET.dump(xml_root)

输出文件:

<?xml version='1.0' encoding='utf-8'?>
<qm:testcase xmlns:qm="http://jazz.net/xmlns/alm/qm/v0.1/">
  <qm:category term="Platform Version" value="Linux_5.4" />
  <category term="Platform Version" value="Linux_6.6" />
</qm:testcase>
© www.soinside.com 2019 - 2024. All rights reserved.