Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 1.91 KB

README.md

File metadata and controls

50 lines (34 loc) · 1.91 KB

XML2JSON

Python script converts XML to JSON or the other way around

Usage

Make this executable

$ chmod u+x xml2json

Then invoke it from the command line like this

$ xml2json -t xml2json -f file.xml file.json

Or the other way around

$ xml2json -t json2xml -f file.json file.xml

You can also output to stdout

$ xml2json -t json2xml -f file.json -o

More info

Relies on ElementTree for the XML parsing. This is based on pesterfish.py but uses a different XML->JSON mapping. The XML->JSON mapping is described here

XML                              JSON
<e/>                             "e": null
<e>text</e>                      "e": "text"
<e name="value" />               "e": { "@name": "value" }
<e name="value">text</e>         "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e>        "e": { "#text": "text", "a": "text" }

This is very similar to the mapping used for Yahoo Web Services

This is a mess in that it is so unpredictable -- it requires lots of testing (e.g. to see if values are lists or strings or dictionaries). For use in Python this could be vastly cleaner. Think about whether the internal form can be more self-consistent while maintaining good external characteristics for the JSON.

Look at the Yahoo version closely to see how it works. Maybe can adopt that completely if it makes more sense...

R. White, 2006 November 6