{"id":13509754,"url":"https://github.com/homanchou/elixir-xml-to-map","last_synced_at":"2026-02-22T11:06:21.442Z","repository":{"id":38858153,"uuid":"74173212","full_name":"homanchou/elixir-xml-to-map","owner":"homanchou","description":"Converts XML String to Elixir Map data structure with string keys","archived":false,"fork":false,"pushed_at":"2024-02-01T21:57:52.000Z","size":54,"stargazers_count":54,"open_issues_count":1,"forks_count":23,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-07T22:41:14.017Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/homanchou.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-18T23:01:32.000Z","updated_at":"2025-04-16T16:37:19.000Z","dependencies_parsed_at":"2024-01-05T22:00:02.962Z","dependency_job_id":"72f6cda9-f7fd-47c2-bfb6-db50386e446d","html_url":"https://github.com/homanchou/elixir-xml-to-map","commit_stats":{"total_commits":28,"total_committers":13,"mean_commits":"2.1538461538461537","dds":0.7142857142857143,"last_synced_commit":"efa92638abd14f39e05ba6675ac3e7c62d5f2d38"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/homanchou/elixir-xml-to-map","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homanchou%2Felixir-xml-to-map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homanchou%2Felixir-xml-to-map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homanchou%2Felixir-xml-to-map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homanchou%2Felixir-xml-to-map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/homanchou","download_url":"https://codeload.github.com/homanchou/elixir-xml-to-map/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homanchou%2Felixir-xml-to-map/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29710317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-22T10:34:24.778Z","status":"ssl_error","status_checked_at":"2026-02-22T10:32:23.200Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-08-01T02:01:12.457Z","updated_at":"2026-02-22T11:06:21.403Z","avatar_url":"https://github.com/homanchou.png","language":"Elixir","funding_links":[],"categories":["XML"],"sub_categories":[],"readme":"# XmlToMap\n\n[![Module Version](https://img.shields.io/hexpm/v/elixir_xml_to_map.svg)](https://hex.pm/packages/elixir_xml_to_map)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/elixir_xml_to_map/)\n[![Total Download](https://img.shields.io/hexpm/dt/elixir_xml_to_map.svg)](https://hex.pm/packages/elixir_xml_to_map)\n[![License](https://img.shields.io/hexpm/l/elixir_xml_to_map.svg)](https://github.com/homanchou/elixir_xml_to_map/blob/master/LICENSE)\n[![Last Updated](https://img.shields.io/github/last-commit/homanchou/elixir-xml-to-map.svg)](https://github.com/homanchou/elixir-xml-to-map/commits/master)\n\nThis library provides two functions to create an Elixir Map data structure from an XML string.\n\n### `XmlToMap.naive_map/2`\n\nUsage:\n\n```elixir\nXmlToMap.naive_map(\"\u003cfoo\u003e\u003cbar\u003e123\u003c/bar\u003e\u003c/foo\u003e\")\n```\n\nResults in:\n\n```elixir\n%{\"foo\" =\u003e %{\"bar\" =\u003e \"123\"}}\n```\n\nConverts XML string to an Elixir map with strings for keys, not atoms, since atoms are not garbage collected.\n\nThis tool is inspired by Rails `Hash.from_xml()`.\n\n----\n\nI call the function \"naive\", because there are known short comings and there is some [controversy](https://stackoverflow.com/questions/40650482/how-to-convert-xml-to-a-map-in-elixir) around using a conversion tool like this since XML and Maps are non-isomorphic and there is no standard way to convert all the information from one format to another.  The recommended way to pull specific well structured information from XML is to use something like xpath.  But if you understand the risks and still prefer to convert the whole XML string to a map then this tool is for you!\n\nIt is currently not able to parse XML namespace.  It also can't determine if a child should be a list unless it sees a repeated child.  If and only if nodes are repeated at the same level will they become a list.\n\n```elixir\n# there are two points inside foo, so the value of \"point\" becomes a list. Had \"foo\" only contained one point then there would be no list but instead one nested map\nXmlToMap.naive_map(\"\u003cfoo\u003e\u003cpoint\u003e\u003cx\u003e1\u003c/x\u003e\u003cy\u003e5\u003c/y\u003e\u003c/point\u003e\u003cpoint\u003e\u003cx\u003e2\u003c/x\u003e\u003cy\u003e9\u003c/y\u003e\u003c/point\u003e\u003c/foo\u003e\")\n\n# =\u003e %{\"foo\" =\u003e %{\"point\" =\u003e [%{\"x\" =\u003e \"1\", \"y\" =\u003e \"5\"}, %{\"x\" =\u003e \"2\", \"y\" =\u003e \"9\"}]}}\n```\n\nPreviously this package did not handle XML node attributes.\nThe current version takes inspiration from a [go goxml2json package](https://github.com/basgys/goxml2json) and exports attributes in the map while prepending \"-\" so you know they are attributes.\n\nWhenever we encounter an XML node with BOTH attributes and children, we wrap \"#content\" around the node's inner value.\n\nFor example this snippet has a `Height` leaf node with attribute `Units` and a value of `0.50`:\n\n```xml\n\u003cItemDimensions\u003e\n   \u003cHeight Units=\"inches\"\u003e0.50\u003c/Height\u003e\n\u003c/ItemDimensions\u003e\n```\n\nThis would become this snippet:\n\n```json\n...\n\"ItemDimensions\": {\n     \"Height\": {\n           \"#content\": \"0.50\",\n           \"-Units\": \"inches\"\n     }\n}\n```\n\nEmpty tags will have a value of nil.\n\n### `XmlToMap.nested_map/2`\n\nThis function produces arguably more verbose and less straightforward result, but it preserves\nthe order of sequences and might be then instantiated back to _XML_ in an isomorphic way. \n\nIn general, it reflects the tree structure of an input _XML_ per se.\n\nUsage:\n\n```elixir\nXmlToMap.nested_map(\"\u003cfoo\u003e\u003cbar\u003e123\u003c/bar\u003e\u003c/foo\u003e\")\n```\n\nResults in:\n\n```elixir\n%{\n  attributes: [],\n  name: \"foo\",\n  content: %{attributes: [], name: \"bar\", content: \"123\"}\n}\n```\n\nThe function does not make any assumptions about the content and recursively builds the result.\n\n```xml\n\u003cItemDimensions\u003e\n   \u003cHeight Units=\"inches\"\u003e0.50\u003c/Height\u003e\n\u003c/ItemDimensions\u003e\n```\n\nThis would become this snippet:\n\n```elixir\n%{\n  attributes: [],\n  name: \"ItemDimensions\",\n  content: %{attributes: [{\"Units\", \"inches\"}], name: \"Height\", content: \"0.50\"}\n}\n```\n\nWhich might then be converted to _JSON_ of the same shape.\n\nTo make it less verbose, pass `purge_empty: true` as the second parameter to `XmlToMap.nested_map/2`:\n\n```elixir\nXmlToMap.nested_map(\"\u003cfoo\u003e\u003cbar arg='yes'\u003e123\u003c/bar\u003e\u003cbaz/\u003e\u003c/foo\u003e\", purge_empty: true)\n```\n\nResults in:\n\n```elixir\n%{\n  name: \"foo\",\n  content: [\n    %{attributes: [{\"arg\", \"yes\"}], name: \"bar\", content: \"123\"},\n    %{name: \"baz\"}\n  ]\n}\n```\n\n-----\n\nThere is a dependency on Erlsom to parse XML then converts the 'simple_form' structure into a map.\n\nI prefer Erlsom because it is the best documented erlang XML parser and because it mentions that it does not produce new atoms during the scanning.\n\nSee tests for some example usage.\n\n----\n\n## Installation\n\nThe package can be installed as:\n\n1.  Add `:elixir_xml_to_map` to your list of dependencies in `mix.exs`:\n\n    ```elixir\n    def deps do\n      [{:elixir_xml_to_map, \"~\u003e 2.0\"}]\n    end\n    ```\n\n2.  Ensure `:elixir_xml_to_map` is started before your application:\n\n    ```elixir\n    def application do\n      [extra_applications: [:elixir_xml_to_map]]\n    end\n    ```\n\n\n## License\n\nCopyright (c) 2016-present, Homan Chou\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhomanchou%2Felixir-xml-to-map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhomanchou%2Felixir-xml-to-map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhomanchou%2Felixir-xml-to-map/lists"}