Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions
XML to JSON conversion via command line tool supporting all xmljson conventions with customizable input/output files and conversion dialects.
Primary command-line interface function for XML to JSON conversion.
def main(*test_args):
"""
Main CLI entry point for xml2json conversion.
Parameters:
- *test_args: For testing purposes, bypasses command line argument parsing
Functionality:
- Parses command line arguments
- Reads XML from input file or stdin
- Converts using specified dialect
- Outputs JSON to file or stdout
"""Parses command-line arguments for XML to JSON conversion configuration.
def parse_args(args=None, in_file=sys.stdin, out_file=sys.stdout):
"""
Parse command-line arguments for XML to JSON conversion.
Parameters:
- args: list, command line arguments (None for sys.argv)
- in_file: file, default input file (default: stdin)
- out_file: file, default output file (default: stdout)
Returns:
tuple: (in_file, out_file, dialect) - configured file handles and dialect instance
"""Module invocation:
python -m xmljson [options] [input_file]Console script (after pip install):
xml2json [options] [input_file]input_file: Input XML file (optional, defaults to stdin)-o, --out_file: Output JSON file (optional, defaults to stdout)-d, --dialect: Conversion dialect (default: parker)dialects = {
'abdera': Abdera,
'badgerfish': BadgerFish,
'cobra': Cobra,
'gdata': GData,
'parker': Parker,
'xmldata': XMLData,
'yahoo': Yahoo
}Available dialect options:
abdera: Abdera conventionbadgerfish: BadgerFish conventioncobra: Cobra conventiongdata: GData conventionparker: Parker convention (default)xmldata: Base XMLData class (default configuration)yahoo: Yahoo conventionBasic conversion (parker dialect, default):
python -m xmljson data.xmlSpecify output file:
python -m xmljson -o output.json data.xmlUse different dialect:
python -m xmljson -d badgerfish data.xmlFrom stdin to stdout:
cat data.xml | python -m xmljsonBadgerfish with output file:
xml2json -d badgerfish -o result.json input.xmlPipeline usage:
curl -s http://example.com/api.xml | xml2json -d gdata | jq '.response.data'python -m xmljson -husage: xmljson [-h] [-o OUT_FILE] [-d {abdera,badgerfish,cobra,gdata,parker,xmldata,yahoo}] [in_file]
positional arguments:
in_file defaults to stdin
optional arguments:
-h, --help show this help message and exit
-o OUT_FILE, --out_file OUT_FILE
defaults to stdout
-d {abdera,badgerfish,cobra,gdata,parker,xmldata,yahoo}, --dialect {abdera,badgerfish,cobra,gdata,parker,xmldata,yahoo}
defaults to parkerThe CLI uses context managers for proper file handling:
from contextlib import closing
with closing(in_file) as in_file, closing(out_file) as out_file:
json.dump(dialect.data(parse(in_file).getroot()), out_file, indent=2)Uses xml.etree.cElementTree (or lxml.etree if available) for XML parsing:
try:
from lxml.etree import parse
except ImportError:
from xml.etree.cElementTree import parseOutput is formatted with 2-space indentation for readability:
json.dump(result, out_file, indent=2)