如何使用 minidom 从非字符串数据类型生成 xml?
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定方法
setAttribute
期望其第二个参数(值)是一个字符串。您可以通过将数据转换为字符串来帮助完成此过程:或者,在调用
setAttribute
时转换为字符串:(当然,必须对
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:or, converting to strings when you call
setAttribute
:(and of course, the same must be done for
num
andtime
).