如何使用 minidom 从非字符串数据类型生成 xml?

发布于 2024-09-28 19:53:40 字数 1415 浏览 0 评论 0原文

如何使用 minidom 从非字符串数据类型生成 xml?我有一种感觉,有人会告诉我事先生成字符串,但这不是我想要的。

from datetime import datetime
from xml.dom.minidom import Document

num = "1109"
bool = "false"
time = "2010-06-24T14:44:46.000"

doc = Document()

Submission = doc.createElement("Submission")
Submission.setAttribute("bool",bool)
doc.appendChild(Submission)

Schedule = doc.createElement("Schedule")
Schedule.setAttribute("id",num)
Schedule.setAttribute("time",time)
Submission.appendChild(Schedule)

print doc.toprettyxml(indent="  ",encoding="UTF-8")

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<Submission bool="false">
  <Schedule id="1109" time="2010-06-24T14:44:46.000"/>
</Submission>

如何获取非字符串数据类型的有效 xml 表示形式?

from datetime import datetime
from xml.dom.minidom import Document

num = 1109
bool = False
time = datetime.now()

doc = Document()

Submission = doc.createElement("Submission")
Submission.setAttribute("bool",bool)
doc.appendChild(Submission)

Schedule = doc.createElement("Schedule")
Schedule.setAttribute("id",num)
Schedule.setAttribute("time",time)
Submission.appendChild(Schedule)

print doc.toprettyxml(indent="  ",encoding="UTF-8")

文件“C:\Python25\lib\xml\dom\minidom.py”,第 299 行,在 _write_data 中 data = data.replace("&", "&").replace("<", "<") AttributeError:“bool”对象没有属性“replace”

How do you generate xml from non string data types using minidom? I have a feeling someone is going to tell me to generate strings before hand, but this is not what I'm after.

from datetime import datetime
from xml.dom.minidom import Document

num = "1109"
bool = "false"
time = "2010-06-24T14:44:46.000"

doc = Document()

Submission = doc.createElement("Submission")
Submission.setAttribute("bool",bool)
doc.appendChild(Submission)

Schedule = doc.createElement("Schedule")
Schedule.setAttribute("id",num)
Schedule.setAttribute("time",time)
Submission.appendChild(Schedule)

print doc.toprettyxml(indent="  ",encoding="UTF-8")

This is the result:

<?xml version="1.0" encoding="UTF-8"?>
<Submission bool="false">
  <Schedule id="1109" time="2010-06-24T14:44:46.000"/>
</Submission>

How do I get valid xml representations of non-string datatypes?

from datetime import datetime
from xml.dom.minidom import Document

num = 1109
bool = False
time = datetime.now()

doc = Document()

Submission = doc.createElement("Submission")
Submission.setAttribute("bool",bool)
doc.appendChild(Submission)

Schedule = doc.createElement("Schedule")
Schedule.setAttribute("id",num)
Schedule.setAttribute("time",time)
Submission.appendChild(Schedule)

print doc.toprettyxml(indent="  ",encoding="UTF-8")

File "C:\Python25\lib\xml\dom\minidom.py", line 299, in _write_data
data = data.replace("&", "&").replace("<", "<")
AttributeError: 'bool' object has no attribute 'replace'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您的好友蓝忘机已上羡 2024-10-05 19:53:40

绑定方法 setAttribute 期望其第二个参数(值)是一个字符串。您可以通过将数据转换为字符串来帮助完成此过程:

bool = str(False)

或者,在调用 setAttribute 时转换为字符串:(

Submission.setAttribute("bool",str(bool))

当然,必须对 num时间)。

The bound method setAttribute expects its second argument, the value, to be a string. You can help the process along by converting the data to strings:

bool = str(False)

or, converting to strings when you call setAttribute:

Submission.setAttribute("bool",str(bool))

(and of course, the same must be done for num and time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文