Python 2 xml.dom - 更改元素前缀

时间:2018-06-18 20:37:34

标签: python xml dom python-2.3

我正在使用较旧版本的Python(2.3)来处理遗留系统,而且我没有可用的ElementTree(从2.5开始......)。 xml.dom包似乎是我解析和编辑XML文档的最佳界面,但是如果你发现这里有不可行或明显错误的东西,请随意引导我向另一个方向发展。

我无法更改我已解析的XML。我想将所有标签设置为具有特定的prefix /命名空间,因此我编写了此函数:

def recursive_set_ns(root):
    # type: (Element) -> None
    """Set namespaces for all tags recursively by DFS."""
    if (root.prefix is None) or (root.prefix == ""):
        root.prefix = "some_prefix"
    if not root.hasChildNodes():
        # Probably not necessary, though good to have for logic.
        return
    for child_tag in root.childNodes:
        recursive_set_ns(child_tag)

由于某种原因,变量root.prefix确实会更新,但是当我使用document.toxmldocument.writexml打印出来时,此更改不会反映在XML文档中。

要提供实际的MCVF,我认为这足以显示我遇到的问题:

from xml.dom import minidom

document_string = "<atag>Some text.</atag>"
document = minidom.parseString(document_string)

# documentElement is the "atag" object here.
document.documentElement.prefix = "pre"

# Expecting to see "<pre:atag>Some text.</pre:atag>"
print(document.toxml())  # instead prints the original document_string

You can demo it here.先谢谢你了!

1 个答案:

答案 0 :(得分:0)

我能够对此进行自我解答。

element.tagName = "pre:" + element.tagName

仅编辑整个标签显然是有效的,所以我这样做了,而不是试图找到一个对我有用的API调用。花了很多时间在文档上才能弄清楚这一点。我现在更改所有代码的代码如下:

def recursive_set_ns(root):
    # type: (Element) -> None
    """Set namespaces for all tags recursively by DFS."""
    if ":" not in root.tagName:  # leave existing namespaces alone
        root.tagName = "pre:" + root.tagName
    children = filter(lambda c: c.nodeType == c.ELEMENT_NODE, root.childNodes)
    for child_tag in children:
        recursive_set_ns(child_tag)