Sử dụng gói dicttoxml để chuyển đổi dictionary Python sang biểu diễn XML.
- Cài đặt gói:
pip3 install dicttoxml - Tạo dictionary:
D1 = {"name": "Ravi", "age": 21, "marks": 55} - Import và chuyển đổi:
>>> from dicttoxml import dicttoxml >>> xml = dicttoxml(D1) >>> xml = xml.decode() >>> xml '
'Ravi 21 55 - Lưu ra file XML:
>>> xmlfile = open("dict.xml", "w") >>> xmlfile.write(xml) >>> xmlfile.close()