How to parse an XML with encoded HTML in Android

jillsea

New Member
I'm trying to parse an XML coming from an php request URI: http://caracasfutbolclub.com/service/news.php. When I do a log after the String xml is parsed, the response is complete, everything is looking good except for the conversion of '<' to '& lt;' and so on with all the HTML's tags (that can be some utf-8 issue or another codification). The real deal is when I'm requesting the elements from the node, the 'title' XML tag is being retrieved as it should but the problem is the 'introtext' tag that is showing only an '<' instead of all the encoded HTML inside of the tag: Note: not only showing, if you log after the "map.put("introtext", XMLfunctions.getValue(e, "introtext"));", you will get that the whole string is only the <. The code that I'm using is the following:MainActivity:\[code\] ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); String xml = XMLfunctions.getXML(); // method that is parsing the whole XML as a String. Document doc = XMLfunctions.XMLfromString(xml); Log.d("XML" , xml); NodeList nodes = doc.getElementsByTagName("New"); for (int i = 0; i < nodes.getLength(); i++) { HashMap<String, String> map = new HashMap<String, String>(); Element e = (Element)nodes.item(i); map.put("title", XMLfunctions.getValue(e, "title")); map.put("introtext", XMLfunctions.getValue(e, "introtext")); map.put("created", "Publicado: " + XMLfunctions.getValue(e, "created")); mylist.add(map); }\[/code\]XMLFuntions:\[code\]public final static Document XMLfromString(String xml){ Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { System.out.println("XML parse error: " + e.getMessage()); return null; } catch (SAXException e) { System.out.println("Wrong XML file structure: " + e.getMessage()); return null; } catch (IOException e) { System.out.println("I/O exeption: " + e.getMessage()); return null; } return doc;}/** Returns element value * @param elem element (it is XML tag) * @return Element value otherwise empty String */ public final static String getElementValue( Node elem ) { Node kid; if( elem != null){ if (elem.hasChildNodes()){ for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){ if( kid.getNodeType() == Node.TEXT_NODE ){ return kid.getNodeValue(); } } } } return ""; } public static String getXML(){ String line = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://caracasfutbolclub.com/service/news.php"); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); line = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (MalformedURLException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } catch (IOException e) { line = "<results status=\"error\"><msg>Can't connect to server</msg></results>"; } return line;}public static String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return XMLfunctions.getElementValue(n.item(0));}}\[/code\]
 
Back
Top