Keeping CDATA sections while parsing through XML

Question:

I am trying to convert existing Xml file to another xml file with adding few nodes. But when i parse my original xml file and write it to the another xml file, it removes all the CDATA from the output xml. How can i avoid it ?

Here is my code:

tree = ET.parse(r'inputData.xml')
root = tree.getroot()
c = ET.Element("c")
c.text = "3"
root.insert(1, c)
tree.write("outputData.xml")

This is my input XML

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Map[]>
<Map srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over&quot; background-color=&quot;rgba(0, 0, 0, 0)&quot; maximum-extent=&quot;-20037508.34,-20037508.34,20037508.34,20037508.34">    
<Style filter-mode="first" name="boundary">
        <Rule>
          <PolygonSymbolizer fill="#000000" fill-opacity="1" />
        </Rule>
      </Style>
      <Layer name="boundary" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
        <StyleName>boundary</StyleName>
        <Datasource>
          <Parameter name="type"><![CDATA[postgis]]></Parameter>
          <Parameter name="table"><![CDATA[("select * from tbl") as path]]></Parameter>
          <Parameter name="key_field"><![CDATA[gid]]></Parameter>
          <Parameter name="geometry_field"><![CDATA[geom]]></Parameter>
          <Parameter name="extent_cache"><![CDATA[auto]]></Parameter>
          <Parameter name="dbname"><![CDATA[centralized2]]></Parameter>
          <Parameter name="host"><![CDATA[localhost]]></Parameter>
          <Parameter name="port"><![CDATA[5433]]></Parameter>
          <Parameter name="user"><![CDATA[postgres]]></Parameter>
          <Parameter name="password"><![CDATA[mysecretpassword]]></Parameter>
        </Datasource>
      </Layer>
</Map>

On creating new XML all CDATA are removed.

Asked By: Ahsan Mukhtar

||

Answers:

If you use lxml, you can specify a parser that keeps CDATA:

import lxml.etree

file_name = r'inputData.xml'
parser = lxml.etree.XMLParser(strip_cdata=False)
tree = lxml.etree.parse(file_name, parser)
root = tree.getroot()
c = lxml.etree.Element("c")
c.text = "3"
root.insert(1, c)
tree.write("outputData.xml")
Answered By: Endre Both

Did this work for any of you? Even after using strip_cdata=False , my new XML file after modification is stripping off CDATA

Answered By: DBExplorer
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.