json-to-xml-converter
is a simple Python library for converting JSON data to XML and vice versa. It allows you to easily convert JSON files to XML format or XML files to JSON format using command-line arguments.
- Converts JSON to XML with customizable root element.
- Converts XML to JSON while handling nested elements and arrays.
- Command-line utility for easy conversion of files.
- Supports both Python scripts and command-line execution.
To use json-to-xml-converter
, you need Python 3 installed. Clone this repository to your local machine:
git clone https://github.com/BaseMax/json-to-xml-converter.git
cd json-to-xml-converter
pip install -r requirements.txt
You can run the converter directly from the command line:
python converter.py --json-to-xml --input input.json --output output.xml
This converts a JSON file to XML. To convert XML to JSON, use:
python converter.py --xml-to-json --input input.xml --output output.json
For example:
$ python converter.py --json-to-xml --input input.json --output output.xml
Converted JSON to XML and saved to output.xml
--json-to-xml
: Convert JSON to XML.--xml-to-json
: Convert XML to JSON.--input
: Path to the input file (either.json
or.xml
).--output
: Path to the output file (either.xml
or.json
).--root
: Name of the root element for XML (optional, default is "root").--version
: Display the version of the converter.
You can also import the library functions directly into your Python code for more control over the conversion:
import json
from converter import json_to_xml, xml_to_json
# Example JSON to XML conversion
json_data = {
"person": {
"name": "John Doe",
"age": 30,
"hobbies": ["reading", "cycling", "hiking"]
}
}
xml_output = json_to_xml(json_data, root_name="data")
print("XML Output:\n", xml_output)
# Example XML to JSON conversion
xml_data = '''
<data>
<person>
<name>John Doe</name>
<age>30</age>
<hobbies>
<item>reading</item>
<item>cycling</item>
<item>hiking</item>
</hobbies>
</person>
</data>
'''
json_output = xml_to_json(xml_data)
print("JSON Output:\n", json.dumps(json_output, indent=4))
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to fork, submit issues, and create pull requests. Contributions are welcome!
2025 Max Base