{"id":13498326,"url":"https://github.com/smart-fun/XmlToJson","last_synced_at":"2025-03-29T01:30:24.320Z","repository":{"id":45803363,"uuid":"73419869","full_name":"smart-fun/XmlToJson","owner":"smart-fun","description":"Android Library for converting XML to JSON and JSON to XML","archived":false,"fork":false,"pushed_at":"2023-02-14T21:00:23.000Z","size":259,"stargazers_count":557,"open_issues_count":10,"forks_count":112,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-08-01T21:47:25.325Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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/smart-fun.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}},"created_at":"2016-11-10T20:50:48.000Z","updated_at":"2024-07-22T01:38:58.000Z","dependencies_parsed_at":"2024-01-18T23:04:49.760Z","dependency_job_id":"7aa90726-910d-4fdf-97a3-a67710439345","html_url":"https://github.com/smart-fun/XmlToJson","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smart-fun%2FXmlToJson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smart-fun%2FXmlToJson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smart-fun%2FXmlToJson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smart-fun%2FXmlToJson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smart-fun","download_url":"https://codeload.github.com/smart-fun/XmlToJson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222435763,"owners_count":16984186,"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":[],"created_at":"2024-07-31T21:00:22.230Z","updated_at":"2024-10-31T15:31:53.235Z","avatar_url":"https://github.com/smart-fun.png","language":"Java","readme":"# XML to JSON for Android #\n\nXML to JSON is an Android Studio Library which converts easily **XML to JSON** and **JSON to XML**.\n\nIt is fully **configurable** so that you can change for example attribute names.\n\nIt is easy to integrate with **gradle**.\n\n## XML to JSON ##\n\n### Basic usage ###\n\nThere are 2 ways to create a **XmlToJson** object: from a **String** or from an **InputStream**.\n\n```java\n    String xmlString;  // some XML String previously created\n    XmlToJson xmlToJson = new XmlToJson.Builder(xmlString).build();\n```\n\nOR\n\n```java\n    AssetManager assetManager = context.getAssets();\n    InputStream inputStream = assetManager.open(\"myFile.xml\");\n    XmlToJson xmlToJson = new XmlToJson.Builder(inputStream, null).build();\n    inputStream.close();\n```\n\nThen you can convert it to a **JSONObject**, a **String**, or a **Formatted String** (with indentation and line breaks).\n\n```java\n    // convert to a JSONObject\n    JSONObject jsonObject = xmlToJson.toJson();\n\n    // convert to a Json String\n    String jsonString = xmlToJson.toString();\n\n    // convert to a formatted Json String\n    String formatted = xmlToJson.toFormattedString();\n```\n\nThats' it. Here is an example of XML...\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cowner\u003eJohn Doe\u003c/owner\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n    \u003cbook id=\"000\"\u003eBook for the dummies\u003c/book\u003e\n\u003c/library\u003e\n```\n\n... converted into JSON\n\n```json\n{  \n   \"library\":{\n      \"owner\": \"John Doe\",\n      \"book\":[  \n         {  \n            \"id\":\"007\",\n            \"content\":\"James Bond\"\n         },\n         {  \n            \"id\":\"000\",\n            \"content\":\"Book for the dummies\"\n         }\n      ]\n   }\n}\n```\n\n### Custom Content names ###\n\nBy default, the content of a XML Tag is converted into a key called \"content\". This name can be changed with a custom one, using **Builder.setContentName**(String contentPath, String replacementName). You can change as many content names as you want.\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n    \u003cbook id=\"000\"\u003eBook for the dummies\u003c/book\u003e\n\u003c/library\u003e\n```\n\n```java\npublic String convertXmlToJson(String xml) {\n    XmlToJson xmlToJson = new XmlToJson.Builder(xml)\n        .setContentName(\"/library/book\", \"title\")\n        .build();\n    return xmlToJson.toString();\n}\n```\n\n```json\n{  \n   \"library\":{  \n      \"book\":[  \n         {  \n            \"id\":\"007\",\n            \"title\":\"James Bond\"\n         },\n         {  \n            \"id\":\"000\",\n            \"title\":\"Book for the dummies\"\n         }\n      ]\n   }\n}\n```\n\n### Custom Attributes names ###\n\nAttributes are converted into key / values in the JSON. The attribute names may conflict with other keys. You can change the name of any attribute, by specifying the path to the attribute and the replacement name, using **Builder.setAttributeName**(String attributePath, String replacementName).\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n    \u003cbook id=\"000\"\u003eBook for the dummies\u003c/book\u003e\n\u003c/library\u003e\n```\n\n```java\npublic String convertXmlToJson(String xml) {\n    XmlToJson xmlToJson = new XmlToJson.Builder(xml)\n        .setAttributeName(\"/library/book/id\", \"code\")\n        .build();\n    return xmlToJson.toString();\n}\n```\n\n```json\n{  \n   \"library\":{  \n      \"book\":[  \n         {  \n            \"code\":\"007\",\n            \"content\":\"James Bond\"\n         },\n         {  \n            \"code\":\"000\",\n            \"content\":\"Book for the dummies\"\n         }\n      ]\n   }\n}\n```\n\n### Force a Tag to be a list ###\n\nIn a XML hierarchy, an entry can have children. For example, \\\u003clibrary\u003e has 2 entries \\\u003cbook\u003e. In case there is only one book, there is no way to know that Book is a list. But you can force it using **Builder.forceList**(String path).\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n\u003c/library\u003e\n```\n\nBy default, the \\\u003cbook\u003e tag is NOT considered as a list\n\n```json\n{  \n   \"library\":{  \n      \"book\":{  \n         \"id\":\"007\",\n         \"content\":\"James Bond\"\n      }\n   }\n}\n```\n\n```java\npublic String convertXmlToJson(String xml) {\n    XmlToJson xmlToJson = new XmlToJson.Builder(xml)\n        .forceList(\"/library/book\")\n        .build();\n    return xmlToJson.toString();\n}\n```\n\nNow \\\u003cbook\u003e is considered as a list:\n\n```json\n{  \n   \"library\":{  \n      \"book\":[  \n         {  \n            \"id\":\"007\",\n            \"content\":\"James Bond\"\n         }\n      ]\n   }\n}\n```\n\n### Force a Tag or Attribute to be an Integer / Long / Double / Boolean ###\n\nBy default the XML attributes or content are processed as Strings. If you want to force them to be another type (like Integer), then use on of these methods **Builder.forceIntegerForPath**(String path), **Builder.forceLongForPath**(String path), **Builder.forceDoubleForPath**(String path) or **Builder.forceBooleanForPath**(String path).\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cowner\u003eJohn Doe\u003c/owner\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n    \u003cbook id=\"000\"\u003eBook for the dummies\u003c/book\u003e\n\u003c/library\u003e\n```\n\n```java\npublic String convertXmlToJson(String xml) {\n    XmlToJson xmlToJson = new XmlToJson.Builder(xml)\n        .Builder.forceIntegerForPath(\"/library/book/id\")\n        .build();\n    return xmlToJson.toString();\n}\n```\n\n```json\n{  \n   \"library\":{\n      \"owner\": \"John Doe\",\n      \"book\":[  \n         {  \n            \"id\":7,\n            \"content\":\"James Bond\"\n         },\n         {  \n            \"id\":0,\n            \"content\":\"Book for the dummies\"\n         }\n      ]\n   }\n}\n```\nHere \"007\" and \"000\" are converted to 7 and 0.\n\nNote that you can use forcexxxForPath methods AND change the attribute or content name for the same path; the methods in the Builder can be combined. The path used in forcexxxForPath methods is the path in the xml before eventually changing its name.\n\n### Skip a Tag or an Attribute ###\n\nIf you are not interrested in getting all the content of the XML, you can skip some Tags or some Attributes. Like for other methods you have to provide the path for the element to skip. You can use **skipTag** and **skipAttribute** as many times as you need.\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003clibrary\u003e\n    \u003cowner\u003eJohn Doe\u003c/owner\u003e\n    \u003cbook id=\"007\"\u003eJames Bond\u003c/book\u003e\n    \u003cbook id=\"000\"\u003eBook for the dummies\u003c/book\u003e\n\u003c/library\u003e\n```\n\n```java\nXmlToJson xmlToJson = new XmlToJson.Builder(xml)\n    .skipTag(\"/library/owner\")\n    .skipAttribute(\"/library/book/id\")\n    .build();    \n```\n\n```json\n{  \n   \"library\":{  \n      \"book\":[  \n         {  \n            \"content\":\"James Bond\"\n         },\n         {  \n            \"content\":\"Book for the dummies\"\n         }\n      ]\n   }\n}\n```\n\n\n## JSON to XML ##\n\n### Basic usage ###\n\nThere are several ways to create a **JsonToXml** object: from a Json **String**, a **JSONObject** or from an **InputStream**.\n\n```java\n    JSONObject jsonObject; // some JSONObject previously created\n    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject).build();\n```\n\nOR\n\n```java\n    String jsonString; // some JSON String previously created\n    JsonToXml jsonToXml = new JsonToXml.Builder(jsonString).build();\n```\n\nOR\n\n```java\n    AssetManager assetManager = context.getAssets();\n    InputStream inputStream = assetManager.open(\"myFile.json\");\n    JsonToXml jsonToXml = new JsonToXml.Builder(inputStream).build();\n    inputStream.close();\n```\n\nThen you can convert it to a XML String or a XML Formatted String (with indentation and line breaks)\n\n```java\n\n    // Converts to a simple XML String\n    String xmlString = jsonToXml.toString();\n\n    // Converts to a formatted XML String\n    int indentationSize = 3;\n    String formattedXml = jsonToXml.toFormattedString(indentationSize);\n```\n\nHere is a JSON example\n\n```json\n{\n    \"owner\": {\n        \"id\": 124,\n        \"name\": \"John Doe\"\n    }\n}\n```\n\nwhich is converted into XML\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cowner\u003e\n    \u003cid\u003e124\u003c/id\u003e\n    \u003cname\u003eJohn Doe\u003c/name\u003e\n\u003c/owner\u003e\n```\n\n\n### Force a TAG to be an parent Attribute ###\n\nYou may want to use XML Attributes instead of TAG content. You can do this by using the **forceAttribute** method. You need to specify the Path to the TAG.\n\n```java\n    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)\n            .forceAttribute(\"/owner/id\")\n            .build();\n```\n\nThe result becomes\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cowner id=\"124\"\u003e\n    \u003cname\u003eJohn Doe\u003c/name\u003e\n\u003c/owner\u003e\n```\n\n### Force a TAG to be a parent Content ###\n\nWhen a Tag has only one child, you may want that child to be the Content for its parent. You can use the **forceContent** method to achieve this.\n\n```java\n    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)\n            .forceAttribute(\"/owner/id\")\n            .forceContent(\"/owner/name\")\n            .build();\n```\n\nThe result becomes\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003cowner id=\"124\"\u003eJohn Doe\u003c/owner\u003e\n```\n\nwhich is very compact :)\n\n## Installation with gradle ##\n\nAdd the following maven{} line to your **PROJECT** build.gradle file\n\n```\nallprojects {\n    repositories {\n        jcenter()\n        maven { url \"https://jitpack.io\" }\t\t// add this line\n    }\n}\n```\n\nAdd the libary dependency to your **APP** build.gradle file\n\n```\ndependencies {\n    implementation 'com.github.smart-fun:XmlToJson:1.5.3'    // add this line\n}\n```\n\n## License ##\n\nCopyright 2016-2023 Arnaud Guyon\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\n\n[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","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmart-fun%2FXmlToJson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmart-fun%2FXmlToJson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmart-fun%2FXmlToJson/lists"}