Android的XML与CDATA任何回报 [英] Android xml with cdata return nothing

查看:253
本文介绍了Android的XML与CDATA任何回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎很奇怪,我不能得到一个节点的值,因为在CDATA ...
这里是我与我读过它的工作setCoalescing解析器:

Seems strange i can't get the value of a node, because is in CDATA... Here's my parser with the setCoalescing that i've read it works:

public class XMLParser {
public String getXmlFromUrl(String url) {
    String xml = null;
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}
public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setCoalescing(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is); 
    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }
    // return DOM
    return doc;
}
public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}
public final String getElementValue( Node elem ) {
     Node child;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                 if( child.getNodeType() == Node.TEXT_NODE  ){
                     return child.getNodeValue();
                 }
             }
         }
     }
     return "";
} 
}

和我的code得到的值:

and my code to get the values:

    // XML
    final String URL = "http://example.com/xml.xml";
    // XML node keys
    final String parente = "dict";
    final String chiave = "key";
    final String stringa = "string";
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);
    NodeList nl = doc.getElementsByTagName(parente);
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        String chiave_trovata = parser.getValue(e, chiave);
        String stringa_trovata = parser.getValue(e, stringa);
        System.out.println("a"+stringa_trovata);
    }

但我stringa_trovata是空的,因为像

but my stringa_trovata is empty, because is like

你能帮帮我吗?

推荐答案

解决简单的评论,如果(child.getNodeType()== Node.TEXT_NODE){

solved simply commenting the if( child.getNodeType() == Node.TEXT_NODE ){

这篇关于Android的XML与CDATA任何回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆