如何在Android中解析带有编码HTML的XML

时间:2012-06-19 13:24:35

标签: android xml parsing

我正在尝试解析来自php请求URI的XML:http://caracasfutbolclub.com/service/news.php。在解析String xml之后执行日志时,响应完成,除了'<'的转换外,一切看起来都很好到'& LT;”等等所有HTML标签(可能是一些utf-8问题或其他编码)。真正的问题是,当我从节点请求元素时,正在检索“标题”XML标记,但问题是“introtext”标记仅显示“<”而不是标签内的所有编码HTML:

注意:不仅要显示,如果你在“map.put(”introtext“,XMLfunctions.getValue(e,”introtext“))之后登录;”,你会得到整个字符串只是<。

我正在使用的代码如下:

MainActivity:

    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);            
    }

XMLFuntions:

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));
}
}

1 个答案:

答案 0 :(得分:0)

将你的html包裹在CDATA警卫中。 e.g。

<myxmltag><![CDATA[<p>html content</p>]]></myxmltag>