0

I am new to python and I want to modify an xml file that I have. Following some guidance I was able to parse the xml file using Element tree as follow:

import xml.etree.ElementTree as ET
tree = ET.parse('Document.xml')
root = tree.getroot()

Next step I want to modify in it like for example delete first 2 lines and delete all tags of "sprit:", then get the new xml file with the fixes

Here is a sample:

<!-- STATUS : PASSED /  OK / Spec2spirit WARNING /    4/4    (100%) /Test OK -->
  <spirit:vendor>company</spirit:vendor>
  <spirit:library>lib</spirit:library>
  <spirit:name>name</spirit:name>
  <spirit:version>1.0</spirit:version>

and here is the result I want:

  <library>lib</library>
  <name>name</name>
  <version>1.0</version>

Thank you in advance!

Imen
  • 43
  • 5
  • Are you sure you need to edit it using XML-operating library? From the desired result it looks like it would be just as good to use simple text operation. In any case sample and result documents are not well-formed. `sprit:` is a namespace, and deleting it would mean basically generating the new XML document – Alexander Pushkarev Apr 29 '20 at 12:30
  • yeah, if removing the first two lines and the occurrences of spirit is all you want, just load the file normally `with open(filename, 'r')` and write the content to a new file – Snow Apr 29 '20 at 12:33
  • I can see your point, maybe I posted a simple example but I have a more complex xml file and I need to repeat similar operations such as delete or replace lines/ tags in different positions . I am just looking what are the basic commands I need to use. thank you – Imen Apr 29 '20 at 12:51

0 Answers0