{"id":20390600,"url":"https://github.com/gotthardp/python-xml2dict","last_synced_at":"2025-08-31T11:41:35.873Z","repository":{"id":57457743,"uuid":"226719222","full_name":"gotthardp/python-xml2dict","owner":"gotthardp","description":"Flexible XML to dict Converter","archived":false,"fork":false,"pushed_at":"2020-01-20T17:54:23.000Z","size":7,"stargazers_count":10,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-29T14:43:24.689Z","etag":null,"topics":["parser","python","xml","xml-parser"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gotthardp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-08T19:23:59.000Z","updated_at":"2024-04-21T10:53:32.000Z","dependencies_parsed_at":"2022-09-06T02:00:57.104Z","dependency_job_id":null,"html_url":"https://github.com/gotthardp/python-xml2dict","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gotthardp%2Fpython-xml2dict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gotthardp%2Fpython-xml2dict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gotthardp%2Fpython-xml2dict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gotthardp%2Fpython-xml2dict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gotthardp","download_url":"https://codeload.github.com/gotthardp/python-xml2dict/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248558138,"owners_count":21124224,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["parser","python","xml","xml-parser"],"created_at":"2024-11-15T03:25:54.112Z","updated_at":"2025-04-12T11:21:59.242Z","avatar_url":"https://github.com/gotthardp.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flexible XML to dict Converter\n\n```bash\npip install python-xml2dict\n```\n\n```python\nimport xml2dict\n```\n\n## Default Behaviour\n\nThe `parse` function takes a string or a file as a parameter. All other\nparameters are optional.\n\n```python\nxml = '''\n    \u003clist\u003e\n        \u003citems\u003e\n            \u003citem id=\"1\"\u003eA\u003c/item\u003e\n            \u003citem id=\"2\"\u003eB\u003c/item\u003e\n            \u003citem id=\"3\"\u003eC\u003c/item\u003e\n            \u003citem id=\"4\"\u003eD\u003c/item\u003e\n        \u003c/items\u003e\n        \u003cdate\u003e1.1.2019\u003c/date\u003e\n    \u003c/list\u003e\n'''\nprint(json.dumps(xml2dict.parse(xml), indent=4))\n```\n\n```python\n{\n    \"list\": {\n        \"items\": {\n            \"item\": [\n                {\n                    \"@id\": \"1\",\n                    \"#text\": \"A\"\n                },\n                {\n                    \"@id\": \"2\",\n                    \"#text\": \"B\"\n                },\n                {\n                    \"@id\": \"3\",\n                    \"#text\": \"C\"\n                },\n                {\n                    \"@id\": \"4\",\n                    \"#text\": \"D\"\n                }\n            ]\n        },\n        \"date\": \"1.1.2019\"\n    }\n}\n```\n\n## Customized Behaviour\n\nBehaviour can be modified by defining custom processing functions, which may be\napplicable to one, more or all XML elements.\n\n### Put items with unique keys to a dict\n\n```python\ndef get_key(obj, name, val, arg):\n    if name == 'id':\n        arg['key'] = val\n    else:\n        obj[tag] = val\n\ndef add_item(obj, tag, val, arg):\n    if tag not in obj:\n        obj[tag] = {arg['key']: val}\n    else:\n        obj[tag][arg['key']] = val\n\nschema = [\n    {\"elem\": [\"item\"], \"attr\": get_key, \"add\": add_item}\n]\n\nprint(json.dumps(xml2dict.parse(xml, schema), indent=4))\n```\n\n```python\n{\n    \"list\": {\n        \"items\": {\n            \"item\": {\n                \"1\": \"A\",\n                \"2\": \"B\",\n                \"3\": \"C\",\n                \"4\": \"D\"\n            }\n        },\n        \"date\": \"1.1.2019\"\n    }\n}\n```\n\n### Use different processing for certain items\n\nIf the processing function does not store the value into a dict, only the\nremaining items will be returned.\n\n```python\ndef print_item(obj, tag, val, arg):\n    print(\"ITEM\", tag, val)\n\nschema = [\n    {\"elem\": [\"item\"], \"add\": print_item}\n]\n\nprint(json.dumps(xml2dict.parse(xml, schema), indent=4))\n```\n\n```python\nITEM item {'@id': '1', '#text': 'A'}\nITEM item {'@id': '2', '#text': 'B'}\nITEM item {'@id': '3', '#text': 'C'}\nITEM item {'@id': '4', '#text': 'D'}\n{\n    \"list\": {\n        \"items\": null,\n        \"date\": \"1.1.2019\"\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgotthardp%2Fpython-xml2dict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgotthardp%2Fpython-xml2dict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgotthardp%2Fpython-xml2dict/lists"}