How do you create xml from non-string data types using a minidisk?

How do you generate xml from non-string data types using minidom? I have a feeling that someone will tell me to create lines in front of my hand, but that’s not what I want.

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 to get valid xml representations of non-string data types?

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 \ miniidom.py", line 299, in _write_data data = data.replace ("&", "&"). replace ("<", "<") AttributeError: object 'bool' does not have attribute 'replace'

+3
source share
1 answer

setAttribute , , , . , :

bool = str(False)

, setAttribute:

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

(, , num time).

+3

Source: https://habr.com/ru/post/1770655/