I want to get the old xml file, manipulate and save it.
Here is my code:
from xml.etree import cElementTree as ET NS = "{http://www.somedomain.com/XI/Traffic/10}" def fix_xml(filename): f = ET.parse(filename) root = f.getroot() eventlist = root.findall("%(ns)Event" % {'ns':NS }) xpath = "%(ns)sEventDetail/%(ns)sEventDescription" % {'ns':NS } for event in eventlist: desc = event.find(xpath) desc.text = desc.text.upper()
The download file contains:
xmlns="http://www.somedomain.com/XI/Traffic/10" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.somedomain.com/XI/Traffic/10 10.xds"
in the root tag.
I have the following namespace issues:
- As you can see, for each tag call, I gave a namespace at the beginning to return the child.
- The generated xml file does not have
<?xml version="1.0" encoding="utf-8"?> At the beginning. - The output tags contain such
<ns0:eventDescription> , while I need the output as the original <eventDescription> , with no namespace at the beginning.
How can they be solved?
source share