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" background-color="rgba(0, 0, 0, 0)" maximum-extent="-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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With