CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xmljson

Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

XML to JSON conversion via command line tool supporting all xmljson conventions with customizable input/output files and conversion dialects.

Capabilities

Main Entry Point

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
    """

Argument Parsing

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
    """

Usage Patterns

Command Line Usage

Module invocation:

python -m xmljson [options] [input_file]

Console script (after pip install):

xml2json [options] [input_file]

Arguments

  • 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)

Supported Dialects

dialects = {
    'abdera': Abdera,
    'badgerfish': BadgerFish,
    'cobra': Cobra,
    'gdata': GData,
    'parker': Parker,
    'xmldata': XMLData,
    'yahoo': Yahoo
}

Available dialect options:

  • abdera: Abdera convention
  • badgerfish: BadgerFish convention
  • cobra: Cobra convention
  • gdata: GData convention
  • parker: Parker convention (default)
  • xmldata: Base XMLData class (default configuration)
  • yahoo: Yahoo convention

Usage Examples

Basic conversion (parker dialect, default):

python -m xmljson data.xml

Specify output file:

python -m xmljson -o output.json data.xml

Use different dialect:

python -m xmljson -d badgerfish data.xml

From stdin to stdout:

cat data.xml | python -m xmljson

Badgerfish with output file:

xml2json -d badgerfish -o result.json input.xml

Pipeline usage:

curl -s http://example.com/api.xml | xml2json -d gdata | jq '.response.data'

Help Output

python -m xmljson -h
usage: 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 parker

Implementation Details

File Handling

The 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)

XML Parsing

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 parse

JSON Output

Output is formatted with 2-space indentation for readability:

json.dump(result, out_file, indent=2)

Error Handling

  • Invalid Dialect: Raises TypeError for unknown dialect names
  • File Errors: Standard file I/O error handling
  • XML Parse Errors: Propagates XML parsing exceptions
  • JSON Errors: Propagates JSON serialization exceptions

Use Cases

  • Batch Processing: Convert multiple XML files to JSON
  • Pipeline Integration: Part of data processing pipelines
  • API Development: Quick XML-to-JSON conversion for API responses
  • Data Migration: Converting XML data stores to JSON format
  • Testing: Comparing XML-to-JSON conversion results across dialects

Install with Tessl CLI

npx tessl i tessl/pypi-xmljson

docs

abdera.md

badgerfish.md

cli.md

cobra.md

gdata.md

index.md

parker.md

yahoo.md

tile.json