Skip to content

Convert XML to JSON with Python 3 and defusedxml or plain ElementTree

License

Notifications You must be signed in to change notification settings

eliask/xml2json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xml2json

Convert XML to JSON with Python 3 and defusedxml or plain ElementTree

Usage

$ ./xml2json.py -h
usage: xml2json.py [-h] [--unsafe] [infile]

Convert an XML file to JSON.

positional arguments:
  infile

optional arguments:
  -h, --help  show this help message and exit
  --unsafe    do not use defusedxml: only for known-safe XML!

Examples

# Normal usage for potentially unsafe XML files:
pip install defusedxml
./xml2json.py sample_input.xml > sample_output.json

# Without defusedxml
./xml2json.py --unsafe sample_input.xml > sample_output.json

# Also supports reading from stdin:
./xml2json.py < sample_input.xml > sample_output.json

Conversion logic

Each element gets converted into an object of the form: { tagName : {@text: optional, @children: optional, ...attributes} }

Input

<rootelem foo="bar">
  some text
  <child1>
    <child2 attr1="baz" attr2="foo"/>
    <child2 attr2="baz" attr3="foo"/>
  </child1>
</rootelem>

Output

{
  "rootelem": {
    "foo": "bar",
    "@text": "some text",
    "@children": [
      {
        "child1": {
          "@children": [
            {
              "child2": {
                "attr1": "baz",
                "attr2": "foo"
              }
            },
            {
              "child2": {
                "attr2": "baz",
                "attr3": "foo"
              }
            }
          ]
        }
      }
    ]
  }
}

About

Convert XML to JSON with Python 3 and defusedxml or plain ElementTree

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages