{"id":25067369,"url":"https://github.com/digitalheir/java-xml-to-json","last_synced_at":"2025-04-14T20:11:56.920Z","repository":{"id":3928357,"uuid":"46452319","full_name":"digitalheir/java-xml-to-json","owner":"digitalheir","description":"👾 convert XML to a structure-preserving JSON representation","archived":false,"fork":false,"pushed_at":"2023-11-14T23:25:57.000Z","size":97,"stargazers_count":16,"open_issues_count":1,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T20:11:49.930Z","etag":null,"topics":["converter","java","json","xml"],"latest_commit_sha":null,"homepage":"","language":"Java","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/digitalheir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-11-18T22:42:08.000Z","updated_at":"2024-05-31T05:57:20.000Z","dependencies_parsed_at":"2023-07-08T17:46:28.272Z","dependency_job_id":null,"html_url":"https://github.com/digitalheir/java-xml-to-json","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalheir%2Fjava-xml-to-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalheir%2Fjava-xml-to-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalheir%2Fjava-xml-to-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/digitalheir%2Fjava-xml-to-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/digitalheir","download_url":"https://codeload.github.com/digitalheir/java-xml-to-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248952350,"owners_count":21188426,"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":["converter","java","json","xml"],"created_at":"2025-02-06T20:55:43.987Z","updated_at":"2025-04-14T20:11:56.895Z","avatar_url":"https://github.com/digitalheir.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XML to terse JSON\n[![GitHub version](https://badge.fury.io/gh/digitalheir%2Fjava-xml-to-json.svg)](https://github.com/digitalheir/java-xml-to-json/releases/latest)\n[![Build Status](https://travis-ci.org/digitalheir/java-xml-to-json.svg?branch=master)](https://travis-ci.org/digitalheir/java-xml-to-json)\n\nThis is a project to convert XML documents to JSON. It does this by taking a disciplined approach\n to create a terse JSON structure. The library has support for node types such as comments and processing instructions.\n \n DTDs are experimentally supported (a node will be created for them), \n but entity references are unpacked, i.e., `\u0026lt;` in XML becomes `\u003c` in JSON. \n\n## Motivation\nThere are a bunch of libraries out there that convert, most notably in [`org.json.XML`](http://www.json.org/javadoc/org/json/XML.html). \nThe problem with most XML-to-JSON implementations is that we lose a lot of information about ordering / attributes / whatever.\n\nAnother motivation was to have a more light-weight JSON serialization of XML, since lossless transformations \n[tend to be wordy](https://github.com/digitalheir/ruby-xml-to-json). In this library, we\ntrade off some readability for a reduction in bytes. JSON output for this library is typically only slightly larger in bytesize \nthan the XML input.\n\n## Usage\nDownload [the latest JAR](https://github.com/digitalheir/java-xml-to-json/releases/latest) or grab from Maven:\n\n```xml\n\u003cdependencies\u003e\n        \u003cdependency\u003e\n            \u003cgroupId\u003eorg.leibnizcenter\u003c/groupId\u003e\n            \u003cartifactId\u003exml-to-json\u003c/artifactId\u003e\n            \u003cversion\u003e0.9.2\u003c/version\u003e\n        \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\nor Gradle:\n```groovy\ncompile 'org.leibnizcenter:xml-to-json:0.9.2'\n```\n\n```java\nimport com.google.gson.Gson;\nimport org.leibnizcenter.xml.helpers.DomHelper;\nimport org.leibnizcenter.xml.NotImplemented;\nimport org.leibnizcenter.xml.TerseJson;\nimport org.w3c.dom.Document;\nimport org.xml.sax.SAXException;\n\nimport javax.xml.parsers.ParserConfigurationException;\nimport java.io.IOException;\n\npublic class Main {\n    private static final TerseJson.WhitespaceBehaviour COMPACT_WHITE_SPACE = TerseJson.WhitespaceBehaviour.Compact;\n\n    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, NotImplemented {\n        String xml = (\"\u003croot\u003e\" +\n                \"\u003c!-- thïs ïs à cómmënt. --\u003e\" +\n                \"  \u003cel ampersand=\\\"    a \u0026amp;    b\\\"\u003e\" +\n                \"    \u003cselfClosing/\u003e\" +\n                \"  \u003c/el\u003e\" +\n                \"\u003c/root\u003e\");\n\n        // Parse XML to DOM\n        Document doc = DomHelper.parse(xml);\n\n        // Convert DOM to terse representation, and convert to JSON\n        TerseJson.Options opts = TerseJson.Options\n                .with(COMPACT_WHITE_SPACE)\n                .and(TerseJson.ErrorBehaviour.ThrowAllErrors);\n\n        Object terseDoc = new TerseJson(opts).convert(doc);\n        String json = new Gson().toJson(terseDoc);\n\n        System.out.println(json);\n    }\n}\n```\n\nproduces\n\n```json\n[9,[{\"1\":\"root\",\"3\":[[8,\" thïs ïs à cómmënt. \"],\" \",{\"1\":\"el\",\"2\":[[\"ampersand\",\"    a \\u0026    b\"]],\"3\":[\" \",{\"1\":\"selfClosing\"},\" \"]}]}]]\n```\n\n## Details\n\n### JavaDoc\nJavaDoc is available at https://digitalheir.github.io/java-xml-to-json/\n\n### Node types\n```json\nvar nodeTypes = { \n    1: \"element\",\n    2: \"attribute\",\n    3: \"text\",\n    4: \"cdata_section\",\n    5: \"entity_reference\",\n    6: \"entity\",\n    7: \"processing_instruction\",\n    8: \"comment\",\n    9: \"document\",\n    10: \"document_type\",\n    11: \"document_fragment\",\n    12: \"notation\"\n}\n```\n\n### Utility functions\n```js\nvar getChildren = function (node) {\n    if (node[0].match(/element|document/) {\n        return node[1];\n    } else {\n        return undefined;\n    }\n};\n\nvar getName = function (node) {\n    if (node[0].match(/element/)){\n      return node[2];\n    } else if(typeof node[0]===\"string\"){\n        return node[0];\n    } else{\n        return undefined;\n    }\n}\n\n```\n\n### Document\nTranslates to a fixed length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|int|[9](#node-types)|\n|1|array|Children; can be any JSON element|\n\n### Element\nTranslates to a variable length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|int|[1](#node-types)|\n|1|String|tag name|\n|2|array|children as any JSON element; may be missing if element does not have children **and** no attributes|\n|3|array|attributes as [key, value]; may be missing if element does not have attributes|\n\n### Attribute\nTranslates to a fixed length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|String|attribute name|\n|1|String|attribute value|\n\n### Text\nText nodes are converted to JSON strings\n\n### Comment\nTranslates to a fixed length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|int|[8](#node-types)|\n|1|String|Comment text|\n\n### CDATA\nTranslates to a fixed length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|int|[4](#node-types)|\n|1|String|attribute value|\n\n### Processing instruction\nTranslates to a fixed length JSON array:\n\n|index|type|description|\n|---|---|---|\n|0|targ|[7](#node-types)|\n|1|String|target|\n|2|String|content|\n\n### Entity Reference\nNot yet implemented\n\n### Entity \nNot yet implemented\n\n### Document type\nNot yet implemented\n\n### Document fragment\nNot yet implemented\n\n### Notation\nNot yet implemented\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalheir%2Fjava-xml-to-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigitalheir%2Fjava-xml-to-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalheir%2Fjava-xml-to-json/lists"}