避免在Python中将<字符写入XML

时间:2019-03-13 12:05:24

标签: python xml python-2.7 elementtree

我正在尝试将此文本值写入XML标记

 <Parameter name="name"><![CDATA[xyzvalue]]></Parameter>

无论何时,我都会将此值设置为此标签的文本。它像这样生成

<Parameter name="name">&lt;![CDATA[xyzvalue]]&gt;</Parameter>

我需要尝试避免代码中的转义字符

ET.SubElement(parameters, "Parameter", name="id").text = unescape("&lt;![CDATA[xyzvalue]]>")

如何避免它像这样写入xml文件?我需要避免在XML中转义字符。

这是我完整的代码:

map = ET.Element("Map")

parameters = ET.SubElement(map, "Parameters")
ET.SubElement(parameters, "Parameter", name="bounds").text = "-180,-85.05112877980659,180,85.05112877980659"
ET.SubElement(parameters, "Parameter", name="center").text = "0,0,2"
ET.SubElement(parameters, "Parameter", name="format").text = "png"
ET.SubElement(parameters, "Parameter", name="minzoom").text = "0"
ET.SubElement(parameters, "Parameter", name="maxzoom").text = "22"
ET.SubElement(parameters, "Parameter", name="scale").text = "1"
ET.SubElement(parameters, "Parameter", name="metatile").text = "2"
ET.SubElement(parameters, "Parameter", name="id").text = unescape("&lt;![CDATA[xyzvalue]]>")
ET.SubElement(parameters, "Parameter", name="_updated").text = "1552288036000"
ET.SubElement(parameters, "Parameter", name="name").text = "<![CDATA[xyzvalue]]>"
ET.SubElement(parameters, "Parameter", name="tilejson").text = "<![CDATA[2.0.0]]>"
ET.SubElement(parameters, "Parameter", name="scheme").text = "<![CDATA[xyz]]>"

indent(map)
tree = ET.ElementTree(map)
tree.write("plotfinder_tiles.xml", xml_declaration=True, encoding='utf-8', method="xml")

这与How to output CDATA using ElementTree不同,在某种程度上,我在创建元素时遇到了这个问题,无法在创建xml之后美化/删除。

1 个答案:

答案 0 :(得分:1)

如果是我,我会用lxml's CDATA class

但是,如果您想坚持使用ElementTree,则可以 重新定义ET._escape_cdata并确保文本不是以<![CDATA[开头并且不以{结尾{1}},然后转义。

示例...

Python 3。#

]]>

XML输出(test.xml;为了易于阅读而精美印刷)

import xml.etree.ElementTree as ET


def escape_cdata(text):
    # escape character data
    try:
        if not text.startswith("<![CDATA[") and not text.endswith("]]>"):
            if "&" in text:
                text = text.replace("&", "&amp;")
            if "<" in text:
                text = text.replace("<", "&lt;")
            if ">" in text:
                text = text.replace(">", "&gt;")
        return text
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)


ET._escape_cdata = escape_cdata

map_elem = ET.Element("Map")

parameters = ET.SubElement(map_elem, "Parameters")
ET.SubElement(parameters, "Parameter", name="bounds").text = "-180,-85.05112877980659,180,85.05112877980659"
ET.SubElement(parameters, "Parameter", name="center").text = "0,0,2"
ET.SubElement(parameters, "Parameter", name="format").text = "png"
ET.SubElement(parameters, "Parameter", name="minzoom").text = "0"
ET.SubElement(parameters, "Parameter", name="maxzoom").text = "22"
ET.SubElement(parameters, "Parameter", name="scale").text = "1"
ET.SubElement(parameters, "Parameter", name="metatile").text = "2"
ET.SubElement(parameters, "Parameter", name="id").text = "<![CDATA[xyzvalue]]>"
ET.SubElement(parameters, "Parameter", name="_updated").text = "1552288036000"
ET.SubElement(parameters, "Parameter", name="name").text = "<![CDATA[xyzvalue]]>"
ET.SubElement(parameters, "Parameter", name="tilejson").text = "<![CDATA[2.0.0]]>"
ET.SubElement(parameters, "Parameter", name="scheme").text = "<![CDATA[xyz]]>"

tree = ET.ElementTree(map_elem)
tree.write("test.xml", xml_declaration=True, encoding='utf-8', method="xml")

更新:Python 2.7的功能

<Map>
    <Parameters>
        <Parameter name="bounds">-180,-85.05112877980659,180,85.05112877980659</Parameter>
        <Parameter name="center">0,0,2</Parameter>
        <Parameter name="format">png</Parameter>
        <Parameter name="minzoom">0</Parameter>
        <Parameter name="maxzoom">22</Parameter>
        <Parameter name="scale">1</Parameter>
        <Parameter name="metatile">2</Parameter>
        <Parameter name="id"><![CDATA[xyzvalue]]></Parameter>
        <Parameter name="_updated">1552288036000</Parameter>
        <Parameter name="name"><![CDATA[xyzvalue]]></Parameter>
        <Parameter name="tilejson"><![CDATA[2.0.0]]></Parameter>
        <Parameter name="scheme"><![CDATA[xyz]]></Parameter>
    </Parameters>
</Map>