Parsing double quotes from xml - android

I have xml file in server. I am parsing this xml using DOM(xml is not big). In one node there is string with double quotes.
<NODE1>hello "world"</NODE1>
When i see this xml url in browser and check its source it looks like this:
<NODE1>hello "world"</NODE1>
So when i parse this value i get string till double quotes. It seems after double quotes parser doesn't go forward. Any help ? I want to use DOM only in my current situation. This xml is used by other platform also apart from android. Like in iPhone its working perfectly. What should I do to read all value in android using DOM.
Thanks.

I'm not sure if I understand your question, but have a look at using CDATA
http://www.globalguideline.com/xml/XML_CDATA.php

A xml that contains double quote in a value is not valid according to specification: http://www.devx.com/tips/Tip/14068. I would suggest that you escape the special characters server side otherwise you will not be able to parse the xml.

Related

Android JSON escape unicode chars

In android I create JSON String like this
jsonObject.toString();
But the problem is that Unicode chars aren't encoded, for example:
{"number":123456,"name":"בית"}
Should be:
{"number":123456,"name":"\u05D1\u05D9\u05EA"}
How do I do that?
I tried different JSON converting libs but couldn't one that does it.
One solution that I found is to escape it using StringEscapeUtils, and remove double slash in the string, like this:
jsonArray.put("name", StringEscapeUtils.escapeJson(name));
jsonArray.toString().replace("\\\\u", "\\u");
But I don't think this is the best solution for this case..

Special characters such as PI, or subscripts on the xml of Android

I am coding a maths app and I want to show special characters such as PI, E, or subscripts and all those things.
I want to show them on the xml file of the layout.
How can I do it?
Thank you guys for all!
You can use the Unicode value for the symbol, preceded by \u. For example, the pi character is "\u03C0"
This site: http://www.dionysia.org/html/entities/symbols.html has list of elements which can be used in xml. Just watch the second element. For example:
square = &#8730
THen you need to conver it. For example:
String symbol = Html.fromHtml(square);
Alternative link is here: http://www.hrupin.com/2011/12/how-to-put-some-special-math-symbols-in-textview-editview-or-other-android-ui-element
The characters in a string resource are unicode. You can include special characters using the \unnnn notation.
There are many places to look up the unicode values on the web. Google found this one for me:
http://inamidst.com/stuff/unidata/

Android: How store url in string.xml resource file?

I'm trying to store a fully qualified url, with also query params:
www.miosito.net?prova&reg=bis
but it's causing a problem because &reg is similar to ® entity and android tell me that and html entity is not well written.
I need this because every locale uses a fully different set of url query param.
I tried with [[CDATA[.. ]] but this syntax disliked by xml parser.
The problem is not with &req but with & itself. For XML/HTML you would have to use & entity (or &), but for URLs you should rather URL-encode (see docs) strings, and in that case said & should be replaced with %26. So your final string should look like:
www.miosito.net?prova%26reg=bis
Store it like this:
<string name="my_url">"www.miosito.net?prova&reg=bis"</string>
Where & is the XML equivelant of the ampersand symbol &.
Percent encoding may do the trick: http://en.wikipedia.org/wiki/Percent-encoding
You'll basically have something like this: www.miosito.net?prova%26reg=bis
You can enclose your url in double quotes, something like :
<string name="my_url">"www.miosito.net?prova&reg=bis"</string>
This is a recommended way to enclose string resources in Android.
Update 1 : Have a look at the following link for more info :
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Update 2:
#WebnetMobile.com : Correct, indeed :)
'&' is being treated a special character by xml and enclosing in quotes doesn't work. I tried out
www.miosito.net?prova%26reg=bis
and it didn't work out either. I even tried enclosing it in quotes but still didn't work. Am I missing something ?
Meanwhile, the following does work :
<string name="my_url">www.miosito.net%1$sprova%2$sreg=bis</string>
and then in code :
Resources resources=getResources();
String url=String.format(resources.getString(R.string.my_url),"?","&") ;
The '%1$s' and '%2$s' are format specifiers, much like what is used in printf in C. '%1$s' is for strings, '%2$d' is for decimal numbers and so on.

extracting strings from KML file

I am extracting strings from KML file, if the string contains special character like !, #, #, ', " etc. its using codes like '
I am not able to extract entire string if it is like above, by calling getNodeValue(). It is terminating the string at special character.
<name>Continue onto Royal's Market</name>
If i extract the string i am getting only ""Continue onto Royal". I want entire string as
Continue onto Royal's Market.
How to achieve this ?? If anybody familiar with this please reply to this one.
Thanks
Your problem has nothing to do with KML but is general for XML parsning:
Don't use getNodeValue(), as there is no guarantee in DOM that text isn't actually split over several nodes.
Try using getTextContent() instead.
You might also have to replace entities, as in: node.getTextContent().replaceAll("'","'");
In general I wouldnt use DOM at all for extracting data.
I'd use the XmlPullParser as its simpler to work with - and parses faster.

Problem while parsing node with html tags

Friend's
I'm parsing an xml file from web service,it contains html tags,when i use the particular node like description ,the parser simply escapes to parse.for example below content
i use to parse it simply skipped from parsing
**<desc>
<![CDATA[<img src=http://www.browndailyherald.com/polopoly_fs/1.2403115!image/2344905136.jpg_gen/thumbnails/100x100/2344905136.jpg><br /><br><p>
Coal to men at Brown, only 11 percent of whom told us they are gay. Our bitter heterosexual female staffers tell us that number must be higher.
...</p>]]>
</desc>**
How can i resolve this issue.please help me.
You should extract contents of CDATA and parse it with HTML parser.

Categories

Resources