0

I am consuming a JSON like the following

[
    {
        "Id": "BT00",
        "Text": "Register"
    },
    {
        "Id": "BT01",
        "Text": "Login"
    },
    {
        "Id": "BT02",
        "Text": "Next"
    },
    {
        "Id": "BT03",
        "Text": "Yes"
    }
]

and I want to put each object inside string.xml as follows

<string name="BT00">Register</string>
<string name="BT01">Login</string>
<string name="BT02">Next</string>
<string name="BT03">Yes</string>

try to do this with the following documentation https://developer.android.com/guide/topics/resources/string-resource.html#StringArray, but does not meet what I want to accomplish

Anyone have an idea how to do it?

Update: A difference of the presented answers, my case is oriented to Android and in specific to modify the instances of the folder "res"

  • unfortunatly the `strings.xml` file cannot be modified during runtime, [as shown here](https://stackoverflow.com/a/9679336/8155603) – Tony Jun 14 '17 at 17:17

3 Answers3

1

Okay, for this you need to perform 2 steps.

  1. Convert JSON object array to string array.

  2. Parse the string array to XML format

This link could help for first step.

Alternatively you can convert JSONobject array to XML. This link could help

Shashank
  • 397
  • 2
  • 7
  • 17
1

There are more the same questions, but for your question, one of the way to convert JSON to XML, for example:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.XML;

public class JsonToXML {

    public static void main(String args[]) throws JSONException {
        JSONArray json = new JSONArray("["
                + "    {"
                + "        \"Id\": \"BT00\","
                + "        \"Text\": \"Register\""
                + "    },"
                + "    {"
                + "        \"Id\": \"BT01\","
                + "        \"Text\": \"Login\""
                + "    },"
                + "    {"
                + "        \"Id\": \"BT02\","
                + "        \"Text\": \"Next\""
                + "    },"
                + "    {"
                + "        \"Id\": \"BT03\","
                + "        \"Text\": \"Yes\""
                + "    }"
                + "]");

        String xml = XML.toString(json);
        System.out.println(xml);
    }
}

OUTPUT:

<array>
    <Text>Register</Text>
    <Id>BT00</Id>
</array>
<array>
    <Text>Login</Text>
    <Id>BT01</Id>
</array>
<array>
    <Text>Next</Text>
    <Id>BT02</Id>
</array>
<array>
    <Text>Yes</Text>
    <Id>BT03</Id>
</array>
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
1

It is not possible to put ANY strings into the string.xml file at runtime. It can only be read from. The only way to do so is for you to declare it in the strings.xml before hand, recompile and then run the app. You should look at other alternatives like saving the string into your SharedPreferences or using SQLite if you want to store this data and persist it.

crymson
  • 1,170
  • 7
  • 12