49

I was wondering if I could place XML within /res/values/strings.xml? I ask this because I am checking for the XML data file for my application, if it does not exist yet then it creates it from the default contents that will be contained as a string resource.

Eclipse tries to change the less than and greater than tags to their corresponding HTML entities when using the GUI to edit the strings. Is eclipse on the right track? Because I should think that it will be written out into my file as HTML entities too. Could I use getText() rather than getString() to convert the entities back into tags?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Olical
  • 39,703
  • 12
  • 54
  • 77

3 Answers3

87

Yes you can, just use CDATA

<string name="stringName1"><![CDATA[<html>bla</html>]]></string>

Alexander Stolz
  • 7,454
  • 12
  • 57
  • 64
  • 2
    Yep, thats exactly what I needed, I know it seems slightly unorthodox, using a string resource for XML, but I am just starting out and I am only writing 20 or so characters. – Olical Dec 20 '10 at 13:39
2

I have done this way:

Put your string in strings.xml

<string name="my_string"><![CDATA[Your long text here]]></string>

How to use:

<TextView
    android:id="@+id/textView"
    android:text="@string/my_string"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Done

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
2

It will obviously not work unless you escape characters in there such as < or > or &.

If you do encode the XML, it should work fine but probably not the best way to do it. I would prefer binary resource.


For putting in string.xml, you may encode using

String encoded = URLEncoder.encode(xml);

And decoding is the opposite.

For binary, you place it in RAW folder and you get a binary stream and turn it to string and load.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • So when writing, `<` for example, should be converted to `<`? And by binary resource, would that be something in the raw folder that you copy over to create your data file? – Olical Dec 20 '10 at 13:34