Monday, April 23, 2012

Android Html.fromHtml() loses the HTML if it starts with <p> tag

i call a web service that returns some HTML which enclosed in an XML envelop... something like:



<xml version="1.0" cache="false">
<text color="white">
<p> Some text <br /> <p>
</text>
</xml>


I use XmlPullParser to parse this XML/HTML. To get the text in element, i do the following:



case XmlPullParser.START_TAG:

xmlNodeName = parser.getName();

if (xmlNodeName.equalsIgnoreCase("text")) {
String color = parser.getAttributeValue(null, "color");
String text = parser.nextText();

if (color.equalsIgnoreCase("white")) {

detail.setDetail(Html.fromHtml(text).toString());

}
}
break;


This works well and gets the text or html in element even if it contains some html tags.



Issue arises when the element's data starts with <p> tag as in above example. in this case the data is lost and text is empty.



How can i resolve this?



EDIT



Thanks to Nik & rajesh for pointing out that my service's response is actually not a valid XML & element not closed properly. But i have no control over the service so i cannot edit whats returned. I wonder if there is something like HTML Agility that can parse any type of malformed HTML or can at least get whats in html tags .. like inside <text> ... </text> in my case?? That would also be good.



OR anything else that i can use to parse what i get from the service will be good as long as its decently implementable.



Excuse me for my bad english





No comments:

Post a Comment