简体   繁体   English

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

[英]Python 2 xml.dom - Changing Element Prefix

I'm working with an older version of Python (2.3) for a legacy system, and I don't have ElementTree available (which was since 2.5...). 我正在为旧版系统使用旧版本的Python(2.3),并且没有可用的ElementTree(从2.5开始……)。 The xml.dom package seems to be the best interface I have for parsing and editing XML documents, but if you find something infeasible or blatantly wrong here, please feel free to steer me in another direction. xml.dom软件包似乎是解析和编辑XML文档的最佳接口,但是如果您发现此处不可行或存在明显错误,请随时引导我。

I'm having trouble changing the XML I've parsed. 我无法更改已解析的XML。 I want to set all of my tags to have a particular prefix /namespace, so I wrote this function: 我想将所有标签设置为具有特定的前缀 / namespace,因此我编写了以下函数:

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)

For some reason, the variable root.prefix does in fact get updated, but when I print it out with document.toxml or document.writexml , this change isn't reflected in the XML document. 由于某种原因,变量root.prefix实际上确实会更新,但是当我使用document.toxmldocument.writexml其打印出来时,此更改未反映在XML文档中。

To give an actual MCVF, I think this is sufficient to display the problem I'm experiencing: 为了给出实际的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. 您可以在此处进行演示。 Thank you in advance! 先感谢您!

I was able to self-answer this. 我能够自我回答。

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

It's apparently valid to just edit the whole tag, so I did, instead of trying to find an API call that'd do it for me. 显然,只编辑整个标记是有效的,所以我这样做了,而不是试图找到一个对我有用的API调用。 It took a lot of staring at documentation to figure this out. 花了很多时间在文档上才能弄清楚这一点。 My code to change all of them now looks like: 现在,我更改所有代码的代码如下:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM