使用 lxml 的 etree 创建文档类型
我想将文档类型添加到我使用 LXML 的 etree 生成的 XML 文档中。
但是我不知道如何添加文档类型。 不能选择对字符串进行硬编码和连接。
我期待着在 etree 中添加 PI 的方式:
pi = etree.PI(...)
doc.addprevious(pi)
但它对我不起作用。 如何使用lxml向xml文档添加a?
I want to add doctypes to my XML documents that I'm generating with LXML's etree.
However I cannot figure out how to add a doctype. Hardcoding and concating the string is not an option.
I was expecting something along the lines of how PI's are added in etree:
pi = etree.PI(...)
doc.addprevious(pi)
But it's not working for me. How to add a to a xml document with lxml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用:
This worked for me:
您可以首先使用文档类型创建文档:
prints:
如果您想将文档类型添加到某些不是使用文档类型创建的 XML,您可以首先使用所需的文档类型创建一个文档类型(如上所述),然后复制您的文档类型-less XML:
prints:
这是您要找的吗?
You can create your document with a doctype to begin with:
prints:
If you want to add a doctype to some XML that wasn't created with one, you can first create one with the desired doctype (as above), then copy your doctype-less XML into it:
prints:
Is that what you're looking for?
PI 实际上是作为“doc”中的前一个元素添加的。 因此,它不是“doc”的子项。 您必须使用“doc.getroottree()”
这是一个示例:
使用 getroottree():
The PI is actually added as a previous element from "doc". Thus, it's not a child of "doc". You must use "doc.getroottree()"
Here is an example:
with getroottree():