0

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

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

我正在使用的代码如下:

主要活动:

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

XML函数:

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

1 回答 1

0

将您的 html 包装在CDATA警卫中。例如

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

于 2012-06-19T13:42:48.627 回答