{"id":18306395,"url":"https://github.com/xinz/sax_map","last_synced_at":"2025-04-06T22:06:26.654Z","repository":{"id":57545964,"uuid":"229705014","full_name":"xinz/sax_map","owner":"xinz","description":"A fast and efficient tool for converting XML to Elixir Map","archived":false,"fork":false,"pushed_at":"2025-02-05T12:22:39.000Z","size":59,"stargazers_count":26,"open_issues_count":0,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T22:49:45.385Z","etag":null,"topics":["elixir","xml-to-map"],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/sax_map","language":"Elixir","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/xinz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2019-12-23T07:59:42.000Z","updated_at":"2025-02-05T12:22:43.000Z","dependencies_parsed_at":"2025-02-28T08:25:00.892Z","dependency_job_id":"b35c199f-7e1a-48a1-8088-648555d627e4","html_url":"https://github.com/xinz/sax_map","commit_stats":{"total_commits":33,"total_committers":1,"mean_commits":33.0,"dds":0.0,"last_synced_commit":"a9ab3e450dd9768b751dfab0e9782addc6fb5af3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinz%2Fsax_map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinz%2Fsax_map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinz%2Fsax_map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xinz%2Fsax_map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xinz","download_url":"https://codeload.github.com/xinz/sax_map/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247557767,"owners_count":20958047,"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":["elixir","xml-to-map"],"created_at":"2024-11-05T15:39:06.897Z","updated_at":"2025-04-06T22:06:26.638Z","avatar_url":"https://github.com/xinz.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SAXMap\n\n[![hex.pm version](https://img.shields.io/hexpm/v/sax_map.svg?v=1)](https://hex.pm/packages/sax_map)\n\nConverts an XML String or an XML file stream to a Map.\n\nBenefit from [Saxy](https://hex.pm/packages/saxy)'s SAX mode, this library has a good conversion efficiency.\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:sax_map, \"~\u003e 1.4\"}\n  ]\nend\n```\n\n## Example\n\n```elixir\n\niex(1)\u003e xml = \"\"\"\n...(1)\u003e \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n...(1)\u003e \u003cthread\u003e\n...(1)\u003e   \u003ctitle\u003eHello\u003c/title\u003e\n...(1)\u003e   \u003citems\u003e\n...(1)\u003e     \u003citem\u003eitem1\u003c/item\u003e\n...(1)\u003e     \u003citem\u003eitem2\u003c/item\u003e\n...(1)\u003e   \u003c/items\u003e\n...(1)\u003e \u003c/thread\u003e\n...(1)\u003e \"\"\"\niex(2)\u003e SAXMap.from_string(xml)\n{:ok,\n %{\n   \"thread\" =\u003e %{\"items\" =\u003e %{\"item\" =\u003e [\"item1\", \"item2\"]}, \"title\" =\u003e \"Hello\"}\n }}\n```\n\nBy default `SAXMap.from_string` will ignore all attributes of elements in the result, if you want to merge the attributes as the child elements, please use `ignore_attribute` option to achieve this:\n\n```\nxml = \"\"\"\n  \u003cthread version=\"1\"\u003e\n    \u003ctitle color=\"red\" font=\"16\"\u003eHello\u003c/title\u003e\n    \u003citems size=\"3\"\u003e\n      \u003citem font=\"12\"\u003eitem1\u003c/item\u003e\n      \u003citem font=\"12\"\u003eitem2\u003c/item\u003e\n      \u003citem font=\"12\"\u003eitem3\u003c/item\u003e\n    \u003c/items\u003e\n  \u003c/thread\u003e\n\"\"\"\n\nSAXMap.from_string(xml, ignore_attribute: false)\n\n{:ok,\n  %{\n    \"thread\" =\u003e %{\n      \"content\" =\u003e %{\n        \"items\" =\u003e %{\n          \"content\" =\u003e %{\n            \"item\" =\u003e [\n              %{\"content\" =\u003e \"item1\", \"font\" =\u003e \"12\"},\n              %{\"content\" =\u003e \"item2\", \"font\" =\u003e \"12\"},\n              %{\"content\" =\u003e \"item3\", \"font\" =\u003e \"12\"}\n            ]\n          },\n          \"size\" =\u003e \"3\"\n        },\n        \"title\" =\u003e %{\"color\" =\u003e \"red\", \"content\" =\u003e \"Hello\", \"font\" =\u003e \"16\"}\n      },\n      \"version\" =\u003e \"1\"\n    }\n  }}\n\nSAXMap.from_string(xml, ignore_attribute: {false, \"@\"})\n{:ok,\n  %{\n    \"thread\" =\u003e %{\n      \"@version\" =\u003e \"1\",\n      \"content\" =\u003e %{\n        \"items\" =\u003e %{\n          \"@size\" =\u003e \"3\",\n          \"content\" =\u003e %{\n            \"item\" =\u003e [\n              %{\"@font\" =\u003e \"12\", \"content\" =\u003e \"item1\"},\n              %{\"@font\" =\u003e \"12\", \"content\" =\u003e \"item2\"},\n              %{\"@font\" =\u003e \"12\", \"content\" =\u003e \"item3\"}\n            ]\n          }\n        },\n        \"title\" =\u003e %{\"@color\" =\u003e \"red\", \"@font\" =\u003e \"16\", \"content\" =\u003e \"Hello\"}\n      }\n    }\n  }}\n```\n\n**Please note**: The `ignore_attribute: false` equals `ignore_attribute: {false, \"\"}`, in this case, the child elements will be automatically naming with `\"content\"` as the key of the key-value pair to distinct this key-value pair is from XML text content or attribute. Currently, the `\"content\"` naming is a reserved keyword, please be careful to distinguish it from the XML node name when transfer an XML to a Map be with XML attributes.\n\n## Benchmark\n\nOnly for your reference, all credit goes to [Saxy](https://hex.pm/packages/saxy), the details of benchmark can be found in the `bench` directory of the repository.\n\nRun:\n\n```bash\nmix run xml_to_map.exs\n```\n\nOutput:\n\n```bash\nOperating System: macOS\nCPU Information: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz\nNumber of Available Cores: 16\nAvailable memory: 32 GB\nElixir 1.12.2\nErlang 24.0.4\n\nBenchmark suite executing with the following configuration:\nwarmup: 2 s\ntime: 10 s\nmemory time: 2 s\nparallel: 1\ninputs: none specified\nEstimated total run time: 42 s\n\nBenchmarking SAXMap.from_string ignore attribute...\nBenchmarking SAXMap.from_string with attribute...\nBenchmarking XmlToMap.naive_map...\n\nName                                          ips        average  deviation         median         99th %\nSAXMap.from_string ignore attribute      105.03 K        9.52 μs   ±129.42%           9 μs          33 μs\nSAXMap.from_string with attribute         96.74 K       10.34 μs   ±110.08%           9 μs          35 μs\nXmlToMap.naive_map                        26.31 K       38.01 μs    ±46.21%          33 μs         105 μs\n\nComparison:\nSAXMap.from_string ignore attribute      105.03 K\nSAXMap.from_string with attribute         96.74 K - 1.09x slower +0.82 μs\nXmlToMap.naive_map                        26.31 K - 3.99x slower +28.49 μs\n\nMemory usage statistics:\n\nName                                   Memory usage\nSAXMap.from_string ignore attribute        14.61 KB\nSAXMap.from_string with attribute          16.69 KB - 1.14x memory usage +2.08 KB\nXmlToMap.naive_map                         40.90 KB - 2.80x memory usage +26.29 KB\n\n**All measurements for memory usage were the same**\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinz%2Fsax_map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxinz%2Fsax_map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxinz%2Fsax_map/lists"}