Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving XML files using ElementTree

I'm trying to develop simple Python (3.2) code to read XML files, do some corrections and store them back. However, during the storage step ElementTree adds this namespace nomenclature. For example:

<ns0:trk>   <ns0:name>ACTIVE LOG</ns0:name> <ns0:trkseg> <ns0:trkpt lat="38.5" lon="-120.2">   <ns0:ele>6.385864</ns0:ele>   <ns0:time>2011-12-10T17:46:30Z</ns0:time> </ns0:trkpt> <ns0:trkpt lat="40.7" lon="-120.95">   <ns0:ele>5.905273</ns0:ele>   <ns0:time>2011-12-10T17:46:51Z</ns0:time> </ns0:trkpt> <ns0:trkpt lat="43.252" lon="-126.453">   <ns0:ele>7.347168</ns0:ele>   <ns0:time>2011-12-10T17:52:28Z</ns0:time> </ns0:trkpt> </ns0:trkseg> </ns0:trk> 

The code snippet is below:

def parse_gpx_data(gpxdata, tzname=None, npoints=None, filter_window=None,                    output_file_name=None):         ET = load_xml_library();      def find_trksegs_or_route(etree, ns):         trksegs=etree.findall('.//'+ns+'trkseg')         if trksegs:             return trksegs, "trkpt"         else: # try to display route if track is missing             rte=etree.findall('.//'+ns+'rte')             return rte, "rtept"      # try GPX10 namespace first     try:         element = ET.XML(gpxdata)     except ET.ParseError as v:         row, column = v.position         print ("error on row %d, column %d:%d" % row, column, v)      print ("%s" % ET.tostring(element))     trksegs,pttag=find_trksegs_or_route(element, GPX10)     NS=GPX10     if not trksegs: # try GPX11 namespace otherwise         trksegs,pttag=find_trksegs_or_route(element, GPX11)         NS=GPX11     if not trksegs: # try without any namespace         trksegs,pttag=find_trksegs_or_route(element, "")         NS=""      # Store the results if requested     if output_file_name:         ET.register_namespace('', GPX11)         ET.register_namespace('', GPX10)         ET.ElementTree(element).write(output_file_name, xml_declaration=True)      return; 

I have tried using the register_namespace, but with no positive result. Are there any specific changes for this version of ElementTree 1.3?

like image 744
ilya1725 Avatar asked Jan 24 '12 06:01

ilya1725


People also ask

How do I save an XML file in Python?

Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. The indent parameter is the indentation of the current node. The addindent parameter is the incremental indentation to use for subnodes of the current one.

What is XML Etree ElementTree in Python?

The xml. etree. ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.


2 Answers

In order to avoid the ns0 prefix the default namespace should be set before reading the XML data.

ET.register_namespace('', "http://www.topografix.com/GPX/1/1") ET.register_namespace('', "http://www.topografix.com/GPX/1/0") 
like image 164
ilya1725 Avatar answered Sep 21 '22 08:09

ilya1725


You need to register all your namespaces before you parse xml file.

For example: If you have your input xml like this and Capabilities is the root of your Element tree.

<Capabilities xmlns="http://www.opengis.net/wmts/1.0"     xmlns:ows="http://www.opengis.net/ows/1.1"     xmlns:xlink="http://www.w3.org/1999/xlink"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:gml="http://www.opengis.net/gml"     xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd"     version="1.0.0"> 

Then you have to register all the namespaces i.e attributes present with xmlns like this:

ET.register_namespace('', "http://www.opengis.net/wmts/1.0") ET.register_namespace('ows', "http://www.opengis.net/ows/1.1") ET.register_namespace('xlink', "http://www.w3.org/1999/xlink") ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance") ET.register_namespace('gml', "http://www.opengis.net/gml") 
like image 25
singingsingh Avatar answered Sep 20 '22 08:09

singingsingh