11

I want to create a string <res> in android as below:

<string name="bmi0">0: BMI≤18.5</string>
<string name="bmi1">1: 18.5<BMI≤24</string>
<string name="bmi2">2: 24<BMI≤27</string>

But i shown the error of "Tag start is not closed".

I tried to put a \ symbol in front, but it doesn't work, it has shown the same error.

<string name="bmi0">0: BMI\≤18.5</string>
<string name="bmi1">1: 18.5<BMI\≤24</string>
<string name="bmi2">2: 24<BMI\≤27</string>

How to escape the special XML symbols ?

lospejos
  • 1,976
  • 3
  • 19
  • 35
user2301281
  • 757
  • 2
  • 12
  • 27

8 Answers8

17

You will have to escape those symbols as following:

  • < will be &lt;
  • > will be &gt;

The character doesn't need escaping.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
3

You have to escape those symbols for XML.

What characters do I need to escape in XML documents?

Community
  • 1
  • 1
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
3

As said, for the < and >, you can use this:

<   &lt;
>   &gt;

You need to do this because the < and > aren't allowed in XML, so you need to escape them. You do that by typing: & + specific code for your character + ;, when you show this string, this will be recognized as being the special character (< or >).

You also wanted the smaller or equal sign (≤), which is afaik allowed in XML, so just use .

These are the characters you have to escape:

"   =   &quot;
'   =   &apos;
<   =   &lt;
>   =   &gt;
&   =   &amp;
Xander
  • 5,487
  • 14
  • 49
  • 77
2

Use this:

<   &lt;
>   &gt;
Piyush
  • 18,895
  • 5
  • 32
  • 63
2

Just my 2 cents: guava 15 introduced Escapers, especially for this situations:

 Escaper escaper = XmlEscapers.xmlAttributeEscaper();
 String result = escaper.escape("<>");
 System.out.println(result); // &lt;&gt;
Eugene
  • 117,005
  • 15
  • 201
  • 306
1

Use Unicode sequences for special characters.

You can find some of them here: http://jrgraphix.net/r/Unicode/2200-22FF

Use it like this:

\u2264

will be converted to

Selvin
  • 6,598
  • 3
  • 37
  • 43
Lovis
  • 9,513
  • 5
  • 31
  • 47
1

use

https://stackoverflow.com/a/4301267/2740014

to escape the strings and then put them into your xml.

StringEscapeUtils.escapeXml(stringHere);

Is the needed thing i think.

Community
  • 1
  • 1
int lawl is over 9000
  • 977
  • 1
  • 15
  • 25
1

Use Unicode

For < use \u003c
For > \u003e and
For \u2264

For getting unicode of any String use the following method

public static String getUnicode(String input){
    String output = "";
    for(char c:input.toCharArray()){
        output +="\\u" + Integer.toHexString(c|0x10000).substring(1) ;
    }
    return output;
}
Selvin
  • 6,598
  • 3
  • 37
  • 43
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
  • 1
    why it has +1? \uxxxx is not for xml – Selvin Dec 13 '13 at 15:08
  • @Selvin this is tagged android, and in strings.xml it 100% works PLUS its the only way to do it with some special chars, like "non breaking-hyphen" \uXXXX is even SUGGESTED BY LINT (e.g if you type "...") – Lovis Dec 13 '13 at 15:12
  • 1
    sure the only way .... ` ` xml notation is XXX; or HHHH; – Selvin Dec 13 '13 at 15:15
  • @selvin yes you're right (plus I just saw, that lint is not suggesting it anymore) BUT still, why is it thing working, if its not for xml? – Lovis Dec 13 '13 at 15:18
  • from The Source http://www.w3.org/TR/REC-xml/#dt-charref ... there is no single word about \u notation – Selvin Dec 13 '13 at 15:19
  • I still don't see why it's _wrong_. It answers the question and works in android. (why? Probably because the string is first parsed and _then_ translated) – Lovis Dec 13 '13 at 15:24