Repository: hay/xml2json
Branch: master
Commit: c01922797ebf
Files: 8
Total size: 14.0 KB
Directory structure:
gitextract_41pxzxjy/
├── .gitignore
├── LICENSE
├── README.md
├── setup.py
├── test/
│ ├── __init__.py
│ ├── test_xml2json.py
│ └── xml_ns2.xml
└── xml2json.py
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
*pyc
__pycache__/*
================================================
FILE: LICENSE
================================================
Copyright (C) 2010-2013, Hay Kranen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
================================================
FILE: README.md
================================================
XML2JSON
========
**This module is deprecated and will not be updated anymore (May 2019)**
* To convert between text-based data formats (including XML and JSON) use my library [`dataknead`](http://github.com/hay/dataknead).
* To work with XML in Python i recommend the excellent [`xmltodict`](https://github.com/martinblech/xmltodict).
## Deprecated documentation
Python script converts XML to JSON or the other way around
Usage
-----
Make this executable
$ chmod +x xml2json
Then invoke it from the command line like this
$ xml2json -t xml2json -o file.json file.xml
Or the other way around
$ xml2json -t json2xml -o file.xml file.json
Without the `-o` parameter `xml2json` writes to stdout
$ xml2json -t json2xml file.json
Additional the options:
Strip text (#text and #tail) in the json
$ xml2json -t xml2json -o file.json file.xml --strip_text
Strip namespace in the json
$ xml2json -t xml2json -o file.json file.xml --strip_namespace
In code
from xml2json import json2xml
d = {'r': {'@p': 'p1', '#text': 't1', 'c': 't2'}}
print(json2xml(d))
> b'<r p="p1">t1<c>t2</c></r>'
Installation
------------
Either clone this repo or use `pip` like this:
pip install https://github.com/hay/xml2json/zipball/master
License
-------
xml2json is released under the terms of the [MIT license](http://opensource.org/licenses/MIT).
Contributors
------------
This script was originally written by R.White, Rewritten to a command line utility by [Hay Kranen](http://www.haykranen.nl) with contributions from George Hamilton (gmh04) and Dan Brown (jdanbrown)
Links
------
* [Github](http://github.com/hay/xml2json)
How it works
------------
xml2json relies on ElementTree for the XML parsing. This is based on
pesterfish.py but uses a different XML->JSON mapping.
The XML -> JSON mapping is described [here](http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html)
<pre>
XML JSON
<e/> "e": null
<e>text</e> "e": "text"
<e name="value" /> "e": { "@name": "value" }
<e name="value">text</e> "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e> "e": { "#text": "text", "a": "text" }
</pre>
This is very similar to the mapping used for [Yahoo Web Services](http://developer.yahoo.com/common/json.html#xml)
This is a mess in that it is so unpredictable -- it requires lots of testing (e.g. to see if values are lists or strings or dictionaries). For use in Python this could be vastly cleaner. Think about whether the internal form can be more self-consistent while maintaining good external characteristics for the JSON.
Look at the Yahoo version closely to see how it works. Maybe can adopt that completely if it makes more sense...
R. White, 2006 November 6
================================================
FILE: setup.py
================================================
import os
from setuptools import setup, find_packages
setup(
name = "xml2json",
version = "0.1",
author = "Hay Kranen",
author_email = "huskyr@gmail.com",
description = ("Python script converts XML to JSON or the other way around"),
license = "LICENSE",
keywords = "xml json",
url = "http://github.com/hay/xml2json",
#packages=find_packages(exclude=['ez_setup']),
py_modules=['xml2json'],
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
"License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
],
install_requires=[
'simplejson'
],
entry_points={
'console_scripts': [
'xml2json = xml2json:main'
]
}
)
================================================
FILE: test/__init__.py
================================================
================================================
FILE: test/test_xml2json.py
================================================
import unittest
import xml2json
import optparse
import json
import os
xmlstring = ""
options = None
class SimplisticTest(unittest.TestCase):
def setUp(self):
global xmlstring, options
filename = os.path.join(os.path.dirname(__file__), 'xml_ns2.xml')
xmlstring = open(filename).read()
options = optparse.Values({"pretty": False})
def test_default_namespace_attribute(self):
strip_ns = 0
json_string = xml2json.xml2json(xmlstring,options,strip_ns)
# check string
self.assertTrue(json_string.find("{http://www.w3.org/TR/html4/}table") != -1)
self.assertTrue(json_string.find("{http://www.w3.org/TR/html4/}tr") != -1)
self.assertTrue(json_string.find("@class") != -1)
# check the simple name is not exist
json_data = json.loads(json_string)
self.assertFalse("table" in json_data["root"])
def test_strip_namespace(self):
strip_ns = 1
json_string = xml2json.xml2json(xmlstring,options,strip_ns)
json_data = json.loads(json_string)
# namespace is stripped
self.assertFalse(json_string.find("{http://www.w3.org/TR/html4/}table") != -1)
# TODO , attribute shall be kept
#self.assertTrue(json_string.find("@class") != -1)
#print json_data["root"]["table"]
#print json_data["root"]["table"][0]["tr"]
self.assertTrue("table" in json_data["root"])
self.assertEqual(json_data["root"]["table"][0]["tr"]["td"] , ["Apples", "Bananas"])
if __name__ == '__main__':
unittest.main()
================================================
FILE: test/xml_ns2.xml
================================================
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td class="favorite">Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
================================================
FILE: xml2json.py
================================================
#!/usr/bin/env python
"""xml2json.py Convert XML to JSON
Relies on ElementTree for the XML parsing. This is based on
pesterfish.py but uses a different XML->JSON mapping.
The XML->JSON mapping is described at
http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
Rewritten to a command line utility by Hay Kranen < github.com/hay > with
contributions from George Hamilton (gmh04) and Dan Brown (jdanbrown)
XML JSON
<e/> "e": null
<e>text</e> "e": "text"
<e name="value" /> "e": { "@name": "value" }
<e name="value">text</e> "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e> "e": { "#text": "text", "a": "text" }
This is very similar to the mapping used for Yahoo Web Services
(http://developer.yahoo.com/common/json.html#xml).
This is a mess in that it is so unpredictable -- it requires lots of testing
(e.g. to see if values are lists or strings or dictionaries). For use
in Python this could be vastly cleaner. Think about whether the internal
form can be more self-consistent while maintaining good external
characteristics for the JSON.
Look at the Yahoo version closely to see how it works. Maybe can adopt
that completely if it makes more sense...
R. White, 2006 November 6
"""
import json
import optparse
import sys
import os
from collections import OrderedDict
import xml.etree.cElementTree as ET
def strip_tag(tag):
strip_ns_tag = tag
split_array = tag.split('}')
if len(split_array) > 1:
strip_ns_tag = split_array[1]
tag = strip_ns_tag
return tag
def elem_to_internal(elem, strip_ns=1, strip=1):
"""Convert an Element into an internal dictionary (not JSON!)."""
d = OrderedDict()
elem_tag = elem.tag
if strip_ns:
elem_tag = strip_tag(elem.tag)
for key, value in list(elem.attrib.items()):
d['@' + key] = value
# loop over subelements to merge them
for subelem in elem:
v = elem_to_internal(subelem, strip_ns=strip_ns, strip=strip)
tag = subelem.tag
if strip_ns:
tag = strip_tag(subelem.tag)
value = v[tag]
try:
# add to existing list for this tag
d[tag].append(value)
except AttributeError:
# turn existing entry into a list
d[tag] = [d[tag], value]
except KeyError:
# add a new non-list entry
d[tag] = value
text = elem.text
tail = elem.tail
if strip:
# ignore leading and trailing whitespace
if text:
text = text.strip()
if tail:
tail = tail.strip()
if tail:
d['#tail'] = tail
if d:
# use #text element if other attributes exist
if text:
d["#text"] = text
else:
# text is the value if no attributes
d = text or None
return {elem_tag: d}
def internal_to_elem(pfsh, factory=ET.Element):
"""Convert an internal dictionary (not JSON!) into an Element.
Whatever Element implementation we could import will be
used by default; if you want to use something else, pass the
Element class as the factory parameter.
"""
attribs = OrderedDict()
text = None
tail = None
sublist = []
tag = list(pfsh.keys())
if len(tag) != 1:
raise ValueError("Illegal structure with multiple tags: %s" % tag)
tag = tag[0]
value = pfsh[tag]
if isinstance(value, dict):
for k, v in list(value.items()):
if k[:1] == "@":
attribs[k[1:]] = v
elif k == "#text":
text = v
elif k == "#tail":
tail = v
elif isinstance(v, list):
for v2 in v:
sublist.append(internal_to_elem({k: v2}, factory=factory))
else:
sublist.append(internal_to_elem({k: v}, factory=factory))
else:
text = value
e = factory(tag, attribs)
for sub in sublist:
e.append(sub)
e.text = text
e.tail = tail
return e
def elem2json(elem, options, strip_ns=1, strip=1):
"""Convert an ElementTree or Element into a JSON string."""
if hasattr(elem, 'getroot'):
elem = elem.getroot()
if options.pretty:
return json.dumps(elem_to_internal(elem, strip_ns=strip_ns, strip=strip), indent=4, separators=(',', ': '))
else:
return json.dumps(elem_to_internal(elem, strip_ns=strip_ns, strip=strip))
def json2elem(json_data, factory=ET.Element):
"""Convert a JSON string into an Element.
Whatever Element implementation we could import will be used by
default; if you want to use something else, pass the Element class
as the factory parameter.
"""
return internal_to_elem(json.loads(json_data), factory)
def xml2json(xmlstring, options, strip_ns=1, strip=1):
"""Convert an XML string into a JSON string."""
elem = ET.fromstring(xmlstring)
return elem2json(elem, options, strip_ns=strip_ns, strip=strip)
def json2xml(json_data, factory=ET.Element):
"""Convert a JSON string into an XML string.
Whatever Element implementation we could import will be used by
default; if you want to use something else, pass the Element class
as the factory parameter.
"""
if not isinstance(json_data, dict):
json_data = json.loads(json_data)
elem = internal_to_elem(json_data, factory)
return ET.tostring(elem)
def main():
p = optparse.OptionParser(
description='Converts XML to JSON or the other way around. Reads from standard input by default, or from file if given.',
prog='xml2json',
usage='%prog -t xml2json -o file.json [file]'
)
p.add_option('--type', '-t', help="'xml2json' or 'json2xml'", default="xml2json")
p.add_option('--out', '-o', help="Write to OUT instead of stdout")
p.add_option(
'--strip_text', action="store_true",
dest="strip_text", help="Strip text for xml2json")
p.add_option(
'--pretty', action="store_true",
dest="pretty", help="Format JSON output so it is easier to read")
p.add_option(
'--strip_namespace', action="store_true",
dest="strip_ns", help="Strip namespace for xml2json")
p.add_option(
'--strip_newlines', action="store_true",
dest="strip_nl", help="Strip newlines for xml2json")
options, arguments = p.parse_args()
inputstream = sys.stdin
if len(arguments) == 1:
try:
inputstream = open(arguments[0])
except:
sys.stderr.write("Problem reading '{0}'\n".format(arguments[0]))
p.print_help()
sys.exit(-1)
input = inputstream.read()
strip = 0
strip_ns = 0
if options.strip_text:
strip = 1
if options.strip_ns:
strip_ns = 1
if options.strip_nl:
input = input.replace('\n', '').replace('\r','')
if (options.type == "xml2json"):
out = xml2json(input, options, strip_ns, strip)
else:
out = json2xml(input)
if (options.out):
file = open(options.out, 'w')
file.write(out)
file.close()
else:
print(out)
if __name__ == "__main__":
main()
gitextract_41pxzxjy/ ├── .gitignore ├── LICENSE ├── README.md ├── setup.py ├── test/ │ ├── __init__.py │ ├── test_xml2json.py │ └── xml_ns2.xml └── xml2json.py
SYMBOL INDEX (12 symbols across 2 files)
FILE: test/test_xml2json.py
class SimplisticTest (line 10) | class SimplisticTest(unittest.TestCase):
method setUp (line 12) | def setUp(self):
method test_default_namespace_attribute (line 18) | def test_default_namespace_attribute(self):
method test_strip_namespace (line 30) | def test_strip_namespace(self):
FILE: xml2json.py
function strip_tag (line 46) | def strip_tag(tag):
function elem_to_internal (line 55) | def elem_to_internal(elem, strip_ns=1, strip=1):
function internal_to_elem (line 106) | def internal_to_elem(pfsh, factory=ET.Element):
function elem2json (line 147) | def elem2json(elem, options, strip_ns=1, strip=1):
function json2elem (line 160) | def json2elem(json_data, factory=ET.Element):
function xml2json (line 172) | def xml2json(xmlstring, options, strip_ns=1, strip=1):
function json2xml (line 180) | def json2xml(json_data, factory=ET.Element):
function main (line 195) | def main():
Condensed preview — 8 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (15K chars).
[
{
"path": ".gitignore",
"chars": 19,
"preview": "*pyc\n__pycache__/*\n"
},
{
"path": "LICENSE",
"chars": 1059,
"preview": "Copyright (C) 2010-2013, Hay Kranen\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of thi"
},
{
"path": "README.md",
"chars": 3125,
"preview": "XML2JSON\n========\n\n**This module is deprecated and will not be updated anymore (May 2019)**\n\n* To convert between text-b"
},
{
"path": "setup.py",
"chars": 779,
"preview": "import os\n\nfrom setuptools import setup, find_packages\n\nsetup(\n name = \"xml2json\",\n version = \"0.1\",\n author = "
},
{
"path": "test/__init__.py",
"chars": 0,
"preview": ""
},
{
"path": "test/test_xml2json.py",
"chars": 1580,
"preview": "import unittest\nimport xml2json\nimport optparse\nimport json\nimport os\n\nxmlstring = \"\"\noptions = None\n\nclass SimplisticTe"
},
{
"path": "test/xml_ns2.xml",
"chars": 319,
"preview": "<root>\n\n<h:table xmlns:h=\"http://www.w3.org/TR/html4/\">\n <h:tr>\n <h:td class=\"favorite\">Apples</h:td>\n <h:td>Bana"
},
{
"path": "xml2json.py",
"chars": 7452,
"preview": "#!/usr/bin/env python\n\n\"\"\"xml2json.py Convert XML to JSON\n\nRelies on ElementTree for the XML parsing. This is based on"
}
]
About this extraction
This page contains the full source code of the hay/xml2json GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 8 files (14.0 KB), approximately 3.9k tokens, and a symbol index with 12 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.