Skip to content
This repository has been archived by the owner on May 2, 2019. It is now read-only.

Commit

Permalink
JSON order same as XML
Browse files Browse the repository at this point in the history
Output JASON is now in the same order as the input XML file.
  • Loading branch information
Grunde Løvoll committed Feb 7, 2017
1 parent 3a674ef commit 925de7d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions xml2json.py
Expand Up @@ -38,6 +38,7 @@
import optparse
import sys
import os
from collections import OrderedDict

import xml.etree.cElementTree as ET

Expand All @@ -54,7 +55,7 @@ def strip_tag(tag):
def elem_to_internal(elem, strip_ns=1, strip=1):
"""Convert an Element into an internal dictionary (not JSON!)."""

d = {}
d = OrderedDict()
elem_tag = elem.tag
if strip_ns:
elem_tag = strip_tag(elem.tag)
Expand Down Expand Up @@ -112,7 +113,7 @@ def internal_to_elem(pfsh, factory=ET.Element):
Element class as the factory parameter.
"""

attribs = {}
attribs = OrderedDict()
text = None
tail = None
sublist = []
Expand Down Expand Up @@ -152,7 +153,7 @@ def elem2json(elem, options, strip_ns=1, strip=1):
elem = elem.getroot()

if options.pretty:
return json.dumps(elem_to_internal(elem, strip_ns=strip_ns, strip=strip), sort_keys=True, indent=4, separators=(',', ': '))
return json.dumps(elem_to_internal(elem, strip_ns=strip_ns, strip=strip), indent=4, separators=(',', ': '))
else:
return json.dumps(elem_to_internal(elem, strip_ns=strip_ns, strip=strip))

Expand Down

0 comments on commit 925de7d

Please sign in to comment.