将 XML ElementTree 作为字符串转换为 XML 字符串

Converting an XML ElementTree as String to XML String

提问人:ralay 提问时间:10/14/2023 最后编辑:ralay 更新时间:10/14/2023 访问量:55

问:

我需要将 XML ElementTree(我已经拥有的 String)转换为正确的 XML 内容 String。我不确定我的 ElementTree 格式是否正确,这可能是我得到意外结果的原因。

我尝试使用以下代码:

import xml.etree.ElementTree as ET

tree = '_XML_Worksheet(_XML_Group("view" = "presentation","hide-input" = "false","hide-output" = "false","inline-output" = "false","drawlabel" = "true",`_XML_Text-field`("alignment" = "centred","style" = "Text","layout" = "Normal","ok")))'

root = ET.Element(tree)

xml = ET.tostring(root)

print(xml.decode())

但是这段代码给了我一些胡言乱语:

<_XML_Worksheet(_XML_Group("view" = "presentation","hide-input" = "false","hide-output" = "false","inline-output" = "false","drawlabel" = "true",`_XML_Text-field`("alignment" = "centred","style" = "Text","layout" = "Normal","ok"))) />

当这是我所期望的:

<Worksheet><Group view='presentation' hide-input='false' hide-output='false' inline-output='false' drawlabel='true'><Text-field alignment='centred' style='Text' layout='Normal'>ok</Text-field></Group></Worksheet>
python xml 元素树

评论


答: 暂无答案