Ahmad
Ahmad

Reputation: 4244

UTF-8 in XML and JSON Schema

I'm little bit confusing about utf-8 in XML and JSON Schema

I have following array

$array = array(
    array('name'=>'abc', 'text'=>'اسلسصثصض صثصهخه عه☆anton & budi☆' ),
    array('name'=>'xyz', 'text'=>'nice' ),
);

when i convert it to XML it give me this result

<?xml version="1.0"?>
<response>
  <item>
    <name>abc</name>
    <text>&#x627;&#x633;&#x644;&#x633;&#x635;&#x62B;&#x635;&#x636; &#x635;&#x62B;&#x635;&#x647;&#x62E;&#x647; &#x639;&#x647;&#x2606;anton '&lt;&amp;&gt;' budi&#x2606;</text>
  </item>
  <item>
    <name>xyz</name>
    <text>nice</text>
  </item>
</response>

Why the result is not like following :

<?xml version="1.0"?>
<response>
  <item>
    <name>abc</name>
    <text>اسلسصثصض صثصهخه عه☆anton &amp; budi☆</text>
  </item>
  <item>
    <name>xyz</name>
    <text>nice</text>
  </item>
</response>

And When i convert it to JSON it will give me result :

[
  {
    "name": "abc",
    "text": "\u0627\u0633\u0644\u0633\u0635\u062b\u0635\u0636 \u0635\u062b\u0635\u0647\u062e\u0647 \u0639\u0647\u2606anton '<&>' budi\u2606"
  },
  {
    "name": "xyz",
    "text": "nice"
  }
]

and why not like this :

[
  {
    "name": "abc",
    "text": "اسلسصثصض صثصهخه عه☆anton &amp; budi☆"
  },
  {
    "name": "xyz",
    "text": "nice"
  }
]

is that any way to use utf-8 character inside xml or json ? or that's are the standard ?

Upvotes: 1

Views: 1013

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503090

It's probably for the sake of diagnostics and a better likelihood of being transported correctly - systems are generally pretty good at transporting ASCII, but many systems aren't written well when it comes to other encodings.

It should, of course, be possible to transport the UTF-8 encoded form correctly, but I suspect the encoder you're using is simply being conservative. It means you don't need to make sure you get it right at the HTTP level, for example. The main thing is that it will still give the right text overall. Is this causing you some actual problem, or were you just surprised by the use of escaping?

Upvotes: 1

Related Questions