I am trying to read the XML content from file and convert the content in to dictionary then I need to modify the content of the Dictionary and create a new XML from the updated dictionary
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
The above is my sample XML , I need to get the below XML after converting.
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Test User/author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
Sample code I am using
with open("books.xml") as xml_file:
data_dict = xmltodict.parse(xml_file.read())
xml_file.close()
data_dict['catalog']['book'][0]['author']="testuser"
xml = dicttoxml(data_dict,attr_type=False,root=False)
dom = parseString(xml)
print(dom.toprettyxml())
xmlfile = open("newbook.xml", "w")
xmlfile.write(dom.toprettyxml())
xmlfile.close()
I am expected to get back the XML is same format as the source
But with above code , the output is not coming as same
Any suggestions are welcome