Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid, Parsing XML, how to ignor HTML tags?
    primarykey
    data
    text
    <p>In my project I need to parse XML. some items in XML have HTML tags. I tried to remove those tag but i was not successful. The code in activity is:</p> <pre><code>private NewsFeedItemList parseNewsContent() { NewsParserHandler newsParserHandler = null; Log.i("NewsList", "Starting to parse XML..."); try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xr = parser.getXMLReader(); newsParserHandler = new NewsParserHandler(); xr.setContentHandler(newsParserHandler); ByteArrayInputStream is = new ByteArrayInputStream(strServerResponseMsg.getBytes()); xr.parse(new InputSource(is)); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } NewsFeedItemList itemList = newsParserHandler.getNewsList(); // checkLog(itemList); Log.i("NewsList", "Parsing XML finished. Sending result back to caller..."); return itemList; } </code></pre> <p>"strServerResponseMsg" include the of XML info (<a href="http://www.mania.com.my/rss/ManiaTopStoriesFeedFull.aspx?catid=146" rel="nofollow noreferrer">http://www.mania.com.my/rss/ManiaTopStoriesFeedFull.aspx?catid=146</a>)</p> <p>I cam parse all items but those who has html tag will not parse, completely.</p> <p>this is my parser handler:</p> <pre><code>public class NewsParserHandler extends DefaultHandler { private NewsFeedItemList newsFeedItemList; private boolean current = false; private String currentValue = null; /* Because the feed has another "Title", "link" and "pubdate" name in root, * we need to don't let to be stored in arrays. Therefore, we ignore all of * them by incrementing count.*/ private int count = 0; @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); if(current) { currentValue = new String(ch, start, length); if(currentValue==null || currentValue=="" || currentValue==" ") currentValue = "-"; current = false; } } @Override public void startDocument() throws SAXException { super.startDocument(); newsFeedItemList = new NewsFeedItemList(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); current = true; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); current = false; if(localName.equals("title")) { if(count &gt;= 1) newsFeedItemList.setTitle(currentValue); } if(localName.equals("description")) { newsFeedItemList.setDescription(currentValue); } if(localName.equals("fullbody")) { newsFeedItemList.setFullbody(currentValue); } if(localName.equals("link")) { if(count &gt;= 4) newsFeedItemList.setLink(currentValue); } if(localName.equals("pubDate")) { if(count &gt;= 5) newsFeedItemList.setPubDate(currentValue); } if(localName.equals("image")) { newsFeedItemList.setImage(currentValue); } count++; } @Override public void endDocument() throws SAXException { super.endDocument(); } public NewsFeedItemList getNewsList() { return newsFeedItemList; } } </code></pre> <p>I tried to put <code>currentValue = Html.fromHtml(currentValue).toString();</code> in characters() method but nothing takes effect. Also before sending "strServerResponseMsg" I tried to change it to HTML but parser didn't parse anything.</p> <p>I found these topics but their solutions was not worked for me: <a href="https://stackoverflow.com/questions/6502759/how-to-strip-or-escape-html-tags-in-android">How to strip or escape html tags in Android</a> <a href="https://stackoverflow.com/questions/2478503/display-html-formatted-string">Display HTML Formatted String</a></p> <p>I appreciate so much if you can help me. Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload