Android xml with cdata return nothing

情到浓时终转凉″ 提交于 2019-12-12 05:38:13

问题


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

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

but my stringa_trovata is empty, because is like

can you help me?


回答1:


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



来源:https://stackoverflow.com/questions/12527477/android-xml-with-cdata-return-nothing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!