Home > OS >  How to convert xml variable to xml file in python?
How to convert xml variable to xml file in python?

Time:09-29

I convert my dictionary to xml but I cant save that in xml file

from dict2xml import dict2xml

xml = dict2xml(my_dictionary)
print(xml)

CodePudding user response:

You need to make a XML String and then write file-

from xml.dom.minidom import parseString

xml_str = parseString(xml).toprettyxml()

save_path_file = "myfile.xml"  
with open(save_path_file, "w") as f:
    f.write(xml_str)

CodePudding user response:

The following assigns an xml string to your variable xml.

xml = dict2xml(my_dictionary)

You can do the string to a file by doing the following:

from dict2xml import dict2xml
    
xml = dict2xml(my_dictionary)
with open("my_data.xml", 'w') as f:
    f.write(xml)

This will write your xml data to the file my_data.xml

  • Related