quandyfactory / dicttoxml

Simple library to convert a Python dictionary or other native data type into a valid XML string.
GNU General Public License v2.0
382 stars 147 forks source link

Conversion fails when JSON key name is not a legal XML element name #10

Closed dwcramer closed 10 years ago

dwcramer commented 10 years ago

Given the input JSON:

{
   "^.{0,256}$": "foo"
}

I receive the following error:

dcramer@anatine ~/Desktop/json2plainparams/json_schema (master)
$ ./json2xml.py test.json 
Debug mode is on. Events are logged at: dicttoxml.log
test.json
Traceback (most recent call last):
  File "./json2xml.py", line 18, in <module>
    dom = parseString(xml)
  File "/usr/lib/python2.7/xml/dom/minidom.py", line 1931, in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 46

Due to the fact that <^.{0,256}$>foo</^.{0,256}$> would not be well-formed xml.

I would suggest an xml format like foo to avoid this problem.

quandyfactory commented 10 years ago

Thanks for bringing this to my attention. I should be able to commit a fix in the next day or so.

dwcramer commented 10 years ago

Thanks! Btw., in my original message I see that my suggested output was munged because I hadn't escaped the angle brackets. Here's what I was trying to say: "I would suggest an xml format like <key name="^.{0,256}$">foo</key> to avoid this problem. "

quandyfactory commented 10 years ago

Sorry it took a lot longer than I expected, but I finally got around to fixing this issue. It's addressed in version 1.3.2.