简体   繁体   中英

Keeping CDATA sections while parsing through XML

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM