在新的Andr​​oid版本的Andr​​oid XML解析错误 [英] Android Xml parsing error on new android versions

查看:150
本文介绍了在新的Andr​​oid版本的Andr​​oid XML解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析来自资产的文件夹我的xml文件,我想它与Android 2.3.5它的工作原理,但是当我在安卓4.0.4和4.1试了一下它,它不会给任何错误,但我无法得到从XML值。

 公共类PlistParser {    //解析plist,以数组列表中填写
    公众的ArrayList<&DataModel的GT; parsePlist(字符串XML){
        最终的ArrayList<&DataModel的GT; dataModels =新的ArrayList<&DataModel的GT;();        //从资产XML字符串
        最终文档的DOC = XMLfromString(XML);
        最后的节点列表nodes_array = doc.getElementsByTagName(字典);        //从XML文档列表项填写
        对于(INT指数= 0;指数 - LT; nodes_array.getLength();指数++){            最终节点节点= nodes_array.item(指数);            如果(node.getNodeType()== Node.ELEMENT_NODE){
                最终的元素e =(元)nodes_array.item(指数);                最后的节点列表nodeKey = e.getElementsByTagName(钥匙);
                最后的节点列表的nodeValue = e.getElementsByTagName(字符串);
                DataModel的模型=新的DataModel();                的for(int i = 0; I< nodeValue.getLength();我++){                    最后一个要素eleKey =(元)nodeKey.item(I)
                    最后一个要素eleString =(元)nodeValue.item(I)
                    如果(eleString!= NULL){                        字符串strValue的=的getValue(eleString,串);                        如果(的getValue(eleKey,键)。等于(ISIM)){
                            模型=新的DataModel();
                            model.setIsim(strValue中);
                        }否则如果(的getValue(eleKey,键)。等于(turu)){
                            model.setTuru(strValue中);
                        }否则如果(的getValue(eleKey,键)。等于(kalori)){
                            model.setKalori(Float.parseFloat(strValue中));
                        }否则如果(的getValue(eleKey,键)。等于(蛋白质)){
                            model.setProtein(Float.parseFloat(strValue中));
                        }否则如果(的getValue(eleKey,键)。等于(YAG)){
                            model.setYag(Float.parseFloat(strValue中));
                        }
                        否则,如果(的getValue(eleKey,键)。等于(karbonhidrat)){
                            model.setKarbonhidrat(Float.parseFloat(strValue中));
                        }否则如果(的getValue(eleKey,键)。等于(LIF)){
                            model.setLif(Float.parseFloat(strValue中));
                        }否则如果(的getValue(eleKey,键)。等于(aciklama)){
                            如果(strValue的== NULL){
                                strValue中=;
                            }
                            model.setAciklama(strValue中);
                            dataModels.add(模型);
                        }
                    }
                }
            }
        }        返回dataModels;
    }    //创建从XML字符串XML文档对象
    私人文件XMLfromString(字符串XML){
        文档DOC = NULL;        DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        尝试{
            的DocumentBuilder分贝= dbf.newDocumentBuilder();
            InputSource的是=新的InputSource();
            is.setCharacterStream(新StringReader(XML));
            DOC = db.parse(是);
            doc.getDocumentElement()正常化()。
        }赶上(的ParserConfigurationException E){
            的System.out.println(XML解析错误:+ e.getMessage());
            返回null;
        }赶上(SAXException的E){
            的System.out.println(错的XML文件的结构:+ e.getMessage());
            返回null;
        }赶上(IOException异常五){
            的System.out.println(I / O exeption:+ e.getMessage());
            返回null;
        }        返回文档;
    }    //获取从文本节点唯一的价值
    私人字符串getElementValue(节点ELEM){
        节点的孩子;
        如果(ELEM!= NULL){
            如果(elem.hasChildNodes()){
                对于(孩子= elem.getFirstChild();!小子= NULL;孩子= kid.getNextSibling()){
                    如果(kid.getNodeType()== Node.TEXT_NODE){
                        返回kid.getNodeValue();
                    }
                }
            }
        }
        返回;
    }    ///取从XML节点值
    私人字符串的getValue(元素项,字符串str){
        节点列表N = item.getElementsByTagName(STR);
        返回getElementValue(n.item(0));
    }
}

这里的code,它采用了XML的资产。

 私人字符串readPlistFromAssets(){
    StringBuffer的SB =新的StringBuffer();
    BR的BufferedReader = NULL;    尝试{
        BR =新的BufferedReader(新的InputStreamReader(getAssets()开(sample.xml中)));
        串温度;
        而((TEMP = br.readLine())!= NULL)
        sb.append(临时);
    }赶上(IOException异常五){
        e.printStackTrace();
    } {最后
        尝试{
            br.close(); //停止阅读
        }赶上(IOException异常前){
            ex.printStackTrace();
        }
    }
    返回sb.toString();
}


解决方案

我想通了,我需要用的AsyncTask 来做到这一点。

I tried to parse my xml file from assets folder, I tried it with android 2.3.5 it works but when I tried it in android 4.0.4 and 4.1 it it doesn't give any error but I couldn't get values from xml.

    public class PlistParser {

    // parse Plist and fill in arraylist
    public ArrayList<DataModel> parsePlist(String xml) {
        final ArrayList<DataModel> dataModels = new ArrayList<DataModel>();

        //Get the xml string from assets
        final Document doc =  XMLfromString(xml);
        final NodeList nodes_array = doc.getElementsByTagName("dict");

        //Fill in the list items from the XML document          
        for ( int index = 0; index < nodes_array.getLength(); index++ ) {

            final Node node = nodes_array.item(index);

            if ( node.getNodeType() == Node.ELEMENT_NODE ) {
                final Element e = (Element)nodes_array.item(index);

                final NodeList nodeKey = e.getElementsByTagName("key");
                final NodeList nodeValue = e.getElementsByTagName("string");
                DataModel model = new DataModel();

                for (int i=0; i < nodeValue.getLength(); i++) {

                    final Element eleKey = (Element)nodeKey.item(i);
                    final Element eleString = (Element)nodeValue.item(i);


                    if ( eleString != null ) {

                        String strValue = getValue(eleString, "string");

                        if(getValue(eleKey, "key").equals("isim")) {
                            model = new DataModel();
                            model.setIsim(strValue);
                        } else if(getValue(eleKey, "key").equals("turu")) {
                            model.setTuru(strValue);
                        } else if(getValue(eleKey, "key").equals("kalori")) {
                            model.setKalori(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("protein")) {
                            model.setProtein(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("yag")) {
                            model.setYag(Float.parseFloat(strValue));
                        } 
                        else if(getValue(eleKey, "key").equals("karbonhidrat")) {
                            model.setKarbonhidrat(Float.parseFloat(strValue));
                        } else if(getValue(eleKey, "key").equals("lif")) {
                            model.setLif(Float.parseFloat(strValue));
                        }else if(getValue(eleKey, "key").equals("aciklama")) {
                            if ( strValue == null ) {
                                strValue = "";
                            }
                            model.setAciklama(strValue);
                            dataModels.add(model);
                        }
                    }
                }
            }
        }

        return dataModels;
    }

    // Create xml document object from XML String
    private  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);
            doc.getDocumentElement().normalize();
        } 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;
    }

    // fetch value from Text Node only
    private   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 "";
    }

    /// Fetch value from XML Node
    private   String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return getElementValue(n.item(0));
    }
}    

Here's the code that takes xml from assets.

private  String readPlistFromAssets() {
    StringBuffer sb = new StringBuffer();
    BufferedReader br=null;

    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open("sample.xml")));         
        String temp;
        while ((temp = br.readLine()) != null)
        sb.append(temp);
    } catch (IOException e) {       
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return sb.toString();
}

解决方案

I figured out I need to do it with asynctask.

这篇关于在新的Andr​​oid版本的Andr​​oid XML解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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