How to get embbed XML from XML - android

I have a little problem, during sending request to serwer I'm getting xml with embedded xml in it. Application works on Android 2.1 so I can not just use getTextContent() to get value, so I'm using Node.getFirstChild().getNodeValue(); and for standard node with text it works fine but for node with embedded xml in it, it isn't. Does someone have any advice or had the same problem? How to solve it.
BTW: When I checked type of node all of the are 1 so it means ELEMENT_NODE.

What exactly are you receiving from server? Something like;
<?xml version="1.0"?>
<embedded-xml>
<?xml version="1.0"?>
<data/>
</embedded-xml>
Or;
<?xml version="1.0"?>
<embedded-xml>
<data/>
</embedded-xml>
In latter case you can simply retrieve data -node (not value), there's no need to parse it separately, while first case isn't exactly valid afaik.

You can't embed an arbitrary XML document in an XML document because XML documents can include sequences which are illegal with CDATA sections that are required to embed XML in the first place.
If the task is being able to embed, then extract, an arbitrary XML document, you have to encode the XML document, then embed it in a CDATA section. The reason is that the closing tag for CDATA is "]]>" and if the (arbitrary) embedded document contains a CDATA section, the "]]>" tag for that documents CDATA section will close the CDATA section in the parent.
In my experience, the best approach to embedding XML inside of XML is to compress (because Base64 encoding is going to blow it up ...) then Base64 encode. The reason I've taken this approach is because it is fool-proof and the two transforms (some kind of compressor and base64 en/de-coding) are widely available.

Related

Should we always use xml version="1.0" and encoding="utf-8" in XML of Android?

I have a basically question about XML in Android. This line that is shown at the top of XML files <?xml version="1.0" encoding="utf-8"?> is changeable? I mean we can use for example utf-16 or another version of xml in our codes?
No, there are instances in which the XML encoding and version could or should be different. Different versions of XML are, as expected, different, while different encoding will allow for characters not included in traditional "utf-8" encoding.
EDIT: Re: "Thanks for your answer, I want example for it" & "OK, when do we use utf-16? Is there any code with another version of xml?"
UTF-16 doesn't have much use, as I understand; it is mostly just an old system used for backwards compatibility or for a code that maximizes efficiency with 16-bit over 8-bit. You might use UTF-32 as (from Wikipedia) "the Unicode code points are directly indexed."
From another post on StackOverflow regarding XML versions, "You would only need to use version 1.1 if you are using certain non-ASCII characters in identifiers, EBCDIC line ending characters, or control characters (character codes 1 - 31)."

How to escape HTML attributes in strings.xml?

I'm supplying XML strings files to Android developers and confused about escaping rules when using HTML styles.
The documentation here only talks about embedding <b>, <i> and <u> tags without escaping, but it also shows that you can use Html.fromHtml with any string of HTML.
So how should a chunk of HTML like <font color="red">"Quote"</font> appear in the strings.xml file?
As font tags can have attributes and quotes should be escaped, how should attribute quoting be handled?
The following is invalid XML, so clearly this is wrong:
<string name="eg_1"><font color=\"red\">\"Quote\"</font></string>
Should just the XML attributes be left unescaped?
<string name="eg_2"><font color="red">\"Quote\"</font></string>
It's valid XML, but seems wrong to have different escaping rules in the string.
Perhaps CDATA should be used to simply protect the entire string?
<string name="eg_3"><![CDATA[<font color=\"red\">\"Quote\"</font>]]></string>
Or even just escape it?
<string name="eg_4"><font color=\"red\">\"Quote\"</font></string>
So how should a chunk of HTML like "Quote" appear in the strings.xml file?
Use CDATA, per your third sample above.
The first example is an invalid XML (as you said).
The second example is an invalid strings.xml file.
The third example should be like this:
<string name="eg_3"><![CDATA[<font color="red">"Quote"</font>]]></string>
The fourth example is fine.

What's "msgid" and "xliff" in strings.xml file?

Background
Sometimes I see some weird attributes on the "strings.xml" file made by Google's samples, for example, on the chips example (code available here), I can find this strings file of "res/values-en-rGB" (for English-Britain) :
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="more_string" msgid="8495478259330621990">"+<xliff:g id="COUNT">%1$s</xliff:g>"</string>
<string name="copy_email" msgid="7869435992461603532">"Copy email address"</string>
<string name="copy_number" msgid="530057841276106843">"Copy phone number"</string>
<string name="done" msgid="2356320650733788862">"Return"</string>
</resources>
I think both are used only for localized strings, as I never saw them inside "res/values" folder.
The question
What do those attributes mean?
What does the value of "xliff" mean?
When should you use them and what should you put there?
Are they even needed?
Is there any documentation about those things?
On Android Developers Localise your app page, search for xliff in the section named "Mark message parts that should not be translated".
The explanation is as follows:
Often strings contain contain text that should not be translated into
other languages. Common examples might be a piece of code, a
placeholder for a value, a special symbol, or a name. As you prepare
your strings for translation, look for and mark text that should
remain as-is, without translation, so that the translator doesn't
change it.
To mark text that should not be translated, use an
placeholder tag.
The suggestion is that text within the <xliff:g></xliff:g> tags should not be translated. These tags can also provide metadata about the non-translated text.
When you declare a placeholder tag, always add an id attribute that
explains what the placeholder is for. If your apps later replace the
placeholder value, be sure to provide an example attribute to clarify
the expected use.
For more information on the actual xliff tool, rather than how it relates to Android strings, check out the related question:
What does this mean "xmlns:xliff"? XML.

HTML formatting for TextView

I am a bit confused about the 'rules' of when a TextView element displays text in formatted form or not.
A string like
"There are <i>different ways</i> of coding.\n";
displays without any formatting (including the HTML codes) when I code
tvMyTextView.setText("There are <i>different ways</i> of coding.\n");
but when I define the same string in strings.xml and then load
tvMyTextView.setText(R.strings.TestString);
it displays emphasized.
Even more confused I feel when trying to embed URLs in TextView's like here:
"Click here to switch on the red light.\n";
Needless to say I already tried the various property options of TextView - but they don't seem to make much of a difference unless I missed something. In some cases the URL is encoded in the text, in blue color and can be clicked, in others I can see the HTML formatting. In others again, it is color-encoded and the URL seems to be encoded in the text somehow - but nothing happens when I click it. Regarding the embedding of URLs, unlike for the other example with 'simple' HTML formatting, I couldn't even find out a rule so far of when it works and when it doesn't. Can anyone help me to untie the knots in my head..
Actually, From the Android Docs..
public final void setText (CharSequence text)
Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.
But,
public final void setText (int resid)
no more specification on it..
But from Android Resource String docs..
You can add styling to your strings with HTML markup. For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>
Supported HTML elements include:
<b> for bold text.
<i> for italic text.
<u> for underline text.
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.
And about your URL string,...
tvMyTextView.setText(Html.fromHtml("Click here to switch on the red light.\n"));
Also look at this SO Question Set TextView text from html-formatted string resource in XML
and
Android String Resource
tvMyTextView.setText(Html.fromHtml("There are <i>different ways</i> of coding.\n"));
also try
Below link For linkify so automatically website link assign.
http://developer.android.com/resources/articles/wikinotes-linkify.html
To add a few notes to my own questions and after having received the answers so far, I can only conclude that there doesn't seem to be a reliable way that works everywhere.
In some cases - according to my experience, if a formatted URL is part of the plain text and not enclosed by tags (like http://www.poon-world.com , even just " Poon-World.com " seems to work in most cases), simply setting the properties of the TextView seems to be enough and the links will be clickable. However, if links are embedded in HTML tags and supposed to be clickable from some link text, there seems to be no other way than to go with Html.fromHtml(..).
But there are also a few special cases I can't explain: in some activities/layouts, I am using "embedded" URLs and have set the Click-properties mentioned before, don't use Html.fromHtml .. and surprise!, a click on the indeed created links in the text indeed opens the browser, but only after having added the following line in the code in the OnCreate-Event:
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
(I found this trick in another thread, thanks to the author) No idea why, it seems to be the way the string resources are parsed and evaluated by Android. I just mentioned that on top of all that's already been said so that everyone else looking for solutions and gets confused doesn't start to think he's starting to lose his mind - no, just test the approaches mentioned here on this page and one should usually work out.

Android placement of custom XML files

I have a large XML file which is arranged like so:
<item><title>...</title><link>...</link></item>
How can I use parse(new InputSource()); to point to this XML file if it's stored within my project directory and where do I put the XML file?
The best answer ignores your parse(new InputSource()) request. Put it in res/xml/ and use getResources().getXml(). This gives you an XmlPullParser on your data. The big advantage here is that your XML is somewhat pre-parsed during the compile step, and so parsing is about ten times faster at runtime than with normal XML parsers.
If it absolutely positively has to use DOM or SAX, put it in res/raw/, then use getResources().openRawResource() to get an InputStream, which you can wrap in an InputSource.

Categories

Resources