dabo Commit Revision 1730 Date: 2005-12-30 16:32:36 -0800 (Fri, 30 Dec 2005) Author: ed
Changed: U trunk/dabo/lib/xmltodict.py
Log: Added code to properly escape characters that should not be in XML attributes. Also cleaned up some uneven spacing in the created XML.
Diff: Modified: trunk/dabo/lib/xmltodict.py =================================================================== --- trunk/dabo/lib/xmltodict.py 2005-12-30 04:07:38 UTC (rev 1729) +++ trunk/dabo/lib/xmltodict.py 2005-12-31 00:32:36 UTC (rev 1730) @@ -118,13 +118,16 @@ The dictionary must be in the format returned by dicttoxml(), with keys on "attributes", "code", "cdata", "name", and "children". """ - def addQuote(val): - """Add surrounding quotes to the string.""" + def escQuote(val): + """Add surrounding quotes to the string, and escape + any illegal XML characters. + """ if not isinstance(val, basestring): val = str(val) for qt in ('"', "'", '"""', "'''"): if qt not in val: break + val = val.replace("<", "<").replace(">", ">") return "%s%s%s" % (qt, val, qt) att = "" @@ -132,7 +135,7 @@ if dct.has_key("attributes"): for key, val in dct["attributes"].items(): - val = addQuote(val) + val = escQuote(val) att += " %s=%s" % (key, val) ret += "%s<%s%s" % ("\t" * level, dct["name"], att) @@ -161,13 +164,8 @@ ret += "\n" for child in dct["children"]: ret += dicttoxml(child, level+1) - ret += "%s" % ("\t" * level) - ret += "%s</%s>\n" % (("\t" * level), dct["name"]) - if level == 1: - ret += "\n" - if level == 0: if header is None: header = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n"""
©2005 Ed Leafe |