简体   繁体   English

无法显示从XML获得的特殊字符

[英]Can't show the special characters which is got from XML

I have an API to get places. 我有一个获取位置的API。

Using web browser, it shows: 使用网络浏览器,它显示:

<placeID>5</placeID>
<placeName>!@#$%&*?/_"'()-+;</placeName>
<rating>0</rating>
<categoryID>2</categoryID>
</place>

Using HttpGet and outputing to console, it shows: 使用HttpGet并输出到控制台,它显示:

<place>
<placeID>5</placeID>
<placeName>!@#$%&amp;*?/_&quot;'()-+;</placeName>
<rating>0</rating>
<categoryID>2</categoryID>
</place>

Displaying placeName on Android, it shows !@#$% 在Android上显示placeName,它显示!@#$%

I store raw string in database and use htmlspecialchars($placeName) in the API. 我将原始字符串存储在数据库中,并在API中使用htmlspecialchars($ placeName)。

The problem is the same with ?~=\\^[]{}&lt;&gt;:);):(:'(:o:P:$:S , it becomes ?~=\\^[]{}&lt;&gt;:);):(:'(:o:P:$:S in console and ?~=\\^[]{} on Android. 问题与?~=\\^[]{}&lt;&gt;:);):(:'(:o:P:$:S ,它变成?~=\\^[]{}&lt;&gt;:);):(:'(:o:P:$:S在控制台中,而?~=\\^[]{}在Android上。

I want to show all the special characters on Android just like it is stored in database. 我想显示Android上的所有特殊字符,就像它存储在数据库中一样。

The web browser is showing you the first result (unescaped) because it's converting the &amp; 网络浏览器向您显示第一个结果(未转义),因为它正在转换&amp; to & for display. 并显示。 If you do "View Source" in your browser you will see that it's actually &amp; 如果您在浏览器中执行“查看源代码”,则会看到它实际上是&amp; just like with HttpGet. 就像使用HttpGet一样。 That is exactly what you want happening; 这正是您想要发生的事情; what you see in the console is correct. 您在控制台中看到的内容是正确的。

That said, you shouldn't be having any problems just outputting that string in Android, since it's clearly escaped properly. 就是说,您只需在Android中输出该字符串就不会有任何问题,因为它显然已经正确地转义了。 So, the problem is most likely in your parser, like in this thread: Android SAX parser not getting full text from between tags 因此,问题很可能出现在您的解析器中,如以下线程所示: Android SAX解析器未从标记之间获取全文

Even if you're not using SAXParser, your parsing code is where you should look for a problem at this point, not at the data coming in. 即使您没有使用SAXParser,解析代码也是您此时应该查找问题的地方,而不是输入的数据。

Just to be safe, try also using the ENT_QUOTES flag with htmlspecialchars in your API, although that doesn't seem to be the problem in this specific case. 为了安全起见,请尝试在API中的htmlspecialchars中使用ENT_QUOTES标志,尽管在这种情况下这似乎不是问题。

I should also mention that once you get that problem solved, depending on how you're displaying the text on Android it may show up escaped like in console, where you see things like &amp; 我还应该提到,一旦您解决了该问题,就取决于您在Android上显示文本的方式,它可能会像在控制台中那样以逸出的形式出现,在那里您会看到&amp; instead of &. 代替 &。 If that's the case, you will need to then decode the string from the XML like in this thread: Java: How to unescape HTML character entities in Java? 如果是这样,您将需要像下面的线程一样从XML解码字符串: Java:如何在Java中取消对HTML字符实体的转义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM