本文介绍了避免写&lt; python中的XML字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试将此文本值写入XML标记I am trying to write this text value to the XML tag like this <Parameter name="name"><![CDATA[xyzvalue]]></Parameter>无论何时,我都将此值设置为此标记的文本。它会这样生成Whenever, i am setting this value as text to this tag. It generates it like this <Parameter name="name">&lt;![CDATA[xyzvalue]]&gt;</Parameter>我需要避免代码中的转义字符I need have tried to avoid unescape characters in the code ET.SubElement(parameters, "Parameter", name="id").text = unescape("&lt;![CDATA[xyzvalue]]>")我如何避免将其像这样写入xml文件?我需要避免在XML中转义字符。How can i avoid it writing like this to the xml file ? I need to avoid escaping characters in the 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") 这与如何使用ElementTree输出CDATA ,以某种方式,我遇到了这个问题This is different to How to output CDATA using ElementTree, in a way, that i have this issue at the Element creation not to prettify/ remove after xml has been created.推荐答案如果是我,我会使用 lxml的 CDATA 类。If it were me, I'd use lxml's CDATA class.但是,如果您想坚持使用ElementTree,可以可以重新定义 ET._escape_cdata ,并确保文本不以<![CDATA [且不以]]>结尾However, if you wanted to stick with ElementTree you could probably redefine ET._escape_cdata and make sure the text doesn't start with <![CDATA[ and doesn't end with ]]> before escaping.示例... Python 3。# import xml.etree.ElementTree as ETdef 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_cdatamap_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") XML输出(test.xml;为了易于阅读而精美印刷)XML Output (test.xml; pretty printed for readability)<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> 更新:Python 2.7的功能 def escape_cdata(text, encoding): # 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.encode(encoding, "xmlcharrefreplace") except (TypeError, AttributeError): ET._raise_serialization_error(text) 这篇关于避免写&lt; python中的XML字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 02:45