last day (14 days later) » 

1:22 PM
0
A: Can not append the data into xml file using Django and Python

MaximeKyou need to create your data and append to the file : root = tree.getroot() xmlstr = ET.tostring(root, 'utf-8', method='xml') with open("filename.xml", "a") as myfile: myfile.write(xmlstr) So what i do is creating the tree (your code) and convert to string (tostring()) and just append ...

 
I did as per you but its throwing this NameError at /some/ global name 'ElementTree' is not defined error.
I am using this import xml.etree.cElementTree as ET one
 
so use ET.fromstring instead of ElementTree.tostring
 
I did but its throwing this TypeError at /some/ XML() got an unexpected keyword argument 'encoding' error.
 
i've edited my answer for your need using cElementTree
 
Yes it started working. Thank you. Can you help me for one more functionality.?
 
1:22 PM
i think you need to create another question and make this resolved
tell me
 
No no Actually I am totally new. i need to know how to fetch all data from that file in differnt action and display into table.
 
for event, elem in cElementTree.iterparse(file):
it will parse the file and will go in every element
look at this doc
 
file means here filename.xml right ?
 
a filename or file object containing XML data
 
but here i am getting wrong for mat while appending
<roomlist><locationname name="cuttuck"><roomid name="853112054052"><roomname name="cottage">cottage</roomname><noseats name="12">12</noseats><projectorscreen name="No">No</projectorscreen><videoconf name="Yes">Yes</videoconf></roomid></locationname></roomlist>
<roomlist><locationname name="puri"><roomid name="593513125603"><roomname name="puri room">puri room</roomname><noseats name="30">30</noseats><projectorscreen name="No">No</projectorscreen><videoconf name="No">No</videoconf></roomid></locationname></roomlist>
here i want to append all new data iniside <roomlist></roomlist>
but it is coming twice
Hi
Are you there ?
 
1:50 PM
when you append the data 1st you need to remove the <roomlist> and wrapp all your data around a new <roomlist>
for elem in root.iter():
will give you all roomlist child
 
can you modify it
?
 
2:24 PM
Hi
Are you there ?
 

last day (14 days later) »