Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance 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

AltStyle によって変換されたページ (->オリジナル) /