Advertisement
Fhernd

leer-manipular-guardar-xml.py

Jul 27th, 2018
2,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. from xml.etree.ElementTree import parse, Element
  2.  
  3. documento = parse('datos.xml')
  4.  
  5. raiz = documento.getroot()
  6.  
  7. print(raiz)
  8.  
  9. # Remoción de elementos:
  10. raiz.remove(raiz.find('sri'))
  11. raiz.remove(raiz.find('cr'))
  12.  
  13. # Inserción de un nuevo elemento:
  14. raiz.getchildren().index(raiz.find('nm'))
  15.  
  16. e = Element('spam')
  17. e.text = 'Text, more text'
  18. raiz.insert(2, e)
  19.  
  20. documento.write('datos-nuevos.xml', xml_declaration=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement