I want to update the xml file with the new information using the lxml library. For example, I have this code:
& gt; & Gt; & Gt; From the Lxml Import Entry & gt; & Gt; & Gt; & Gt; & Gt; & Gt; Tree = etree.parse ('books.xml') Where in the 'books.xml' file, this content is:
I have this file with new ones Book I want to update:
& gt; & Gt; & Gt; New_entry = etree.fromstring ("book" = book category = "web" cover = "paperback" & gt; ... & lt; title = lang = "en" & gt; Learning XML 2 & lt; / title & Gt; ... & lt; author & gt; Eric Re & lt; / author & gt; ... & lt; year & gt; 2006 & lt; / year & gt; ... & lt; value & Gt; 49.95 & lt; / price & gt; ... & lt; / book & gt; '' ') My question is, I tree How can I update the tree with the new_entry tree and save the file.
You go here, get root of the tree, add your new element, the tree in the file Save as String:
lxml Import Entry Tree = etree.parse ('books.xml') new_entry = etree.fromstring ('' '' book category = "web" cover = "Paperback" & gt; Title lang = "en" & gt; Learning XML 2 & lt; / title & gt; & lt; Author & gt; Eric Re & lt; / Authors & gt; & lt; Year> 2006 & lt; / year & gt; 49.95 '' ') Tree.getroot () Route. Append ( New_entry) f = open ('books-mod.xml', 'w') f.write (etree.tostring (root, pretty_print = T Ru E)) f.close ()
No comments:
Post a Comment