除了自己创建一种使用 python 编写易于阅读的 XML 的方法之外,还有其他方法吗? xMLFile.write(xmlDom.toxml()) 确实创建了正确的 xml,但读取它们非常困难。我试过 toprettyxml,但似乎作用不大。例如以下是我认为人类可读的 xml:
<myroot>
<ent1
att1="val1"
att2="val2"
att3="val3"
/>
<ent2>
<ent3
att1="val1"
att2="val2"
/>
</ent2>
</myroot>
以下回复的解决方案摘要:
1) 如果您只是偶尔需要阅读它进行调试,那么在编写时不要格式化 xml,而是使用 xmllint 或 xml 编辑器来阅读它。或者
2) 使用像 Beautiful Soup 这样的库。或者
3) 自己写方法
请您参考如下方法:
print lxml.etree.tostring(root_node, pretty_print=True)
