内部存储器中文件的InputStream打开不完整(由于文件中的无效字符?)

时间:2015-03-01 05:43:10

标签: android xml inputstream fileinputstream

为什么下面的代码在解析StringReader时起作用,而不是来自位于内部内存中的文件的InputStream,即使该文件与之前使用的字符串具有完全相同的数据(在代码中注释)?解析器在第二行之后立即到达EOF。当我使用String时,它一直到最后。变量 linha 只是为了在调试期间看到正在解析的行,当我使用InputStream方法时,解析器在文件的第2行到达EOF。

    public boolean read(){
    boolean ret = false;
    try{
        InputStream is =  mContext.openFileInput(fileName);



        if(is!=null) {

            XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
            XmlPullParser myparser = xmlFactoryObject.newPullParser();
            xmlFactoryObject.setNamespaceAware(true);
            myparser.setInput(is,null); //or myparser.setInput(is,"utf-8");
            int event;
            String name = null;
            boolean sai = false;
            event=myparser.getEventType();
            do{
                linha = myparser.getLineNumber();
               if ((event == XmlPullParser.START_TAG)
                   &&((name=myparser.getName()).equals("list-of-commands")))
                       sai = true;
               else
                    event = myparser.next();
            }while(!sai && event!= XmlPullParser.END_DOCUMENT);
            if((name!=null) && myparser.getAttributeValue(null,"machine").equals(modelName)){
                MachineCommand novoParam = null;
                int itemCount = 0;
                while (event != XmlPullParser.END_DOCUMENT) {
                    linha = myparser.getLineNumber();
                    name = myparser.getName();
                    switch (event) {
                        case XmlPullParser.START_TAG:
                            if (name.equals("param")) {
                                itemCount = 0;
                                int numComando =
                                        Integer.parseInt(myparser.getAttributeValue(null,"num_command"));
                                int numParam =
                                        Integer.parseInt(myparser.getAttributeValue(null,"num_param"));
                                novoParam =
                                        ((MachineCommand) progMenu.getExpandableListAdapter()
                                                .getChild(numComando, numParam))
                                                .deepClone(mContext);
                                novoParam.setNumCommand(numComando);
                                novoParam.setNumParam(numParam);
                            } else if (name.equals("spinner")) {
                                int selected =
                                   Integer.parseInt(myparser.getAttributeValue(null,"selected_pos"));
                                Spinner sp = (Spinner)novoParam.getValueAt(itemCount++);
                                sp.setSelection(selected);
                            } else if (name.equals("EditTextNoKB")) {
                                String text = myparser.nextText();
                                EditTextNoKB et = (EditTextNoKB)novoParam.getValueAt(itemCount++);
                                et.setText(text);
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            if (name.equals("param")) {
                                ((ArrayAdapter<MachineCommand>)(dslvProgList.getAdapter())).add(novoParam);
                            }
                    }
                    event = myparser.next();
                }
                if(dslvProgList.getCount()>0) ret = true;
            }
        }
    }catch(Exception e) {
        e.printStackTrace();
        ret = false;
    }


    return ret;

}

要解析的xml文件如下:

&#13;
&#13;
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<list-of-commands machine="1005" version="1.0.0">
  <param num_command="0" num_param="0">Velocidade<EditTextNoKB>6</EditTextNoKB></param>
  <param num_command="0" num_param="2">Sentido<Spinner selected_pos="1">Anti-horário</Spinner></param>
  <param num_command="0" num_param="5">Zerar Contador</param>
  <param num_command="0" num_param="1">Desacelerar<EditTextNoKB>69</EditTextNoKB>voltas</param>
  <param num_command="2" num_param="4">Aguardar Liberar</param>
</list-of-commands>
&#13;
&#13;
&#13;

编辑:正如下面的评论所指出的,我可以说明问题出在文件的写入过程中。也许一些无效的char? 以下是用于在内部存储器中创建文件的代码:

    public boolean write(){
    try {
        FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);


        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(fos, "utf-8");
        serializer.startDocument("utf-8", true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

        serializer.startTag(null, "list-of-commands");
        serializer.attribute(null, "machine", modelName);  //TO DO: Estes dois atributos devem ser
        serializer.attribute(null,"version","1.0.0"); //definidos em arquivo de configuração

        for(int i=0; i<dslvProgList.getCount()-1; i++){
            MachineCommand com = (MachineCommand)dslvProgList.getItemAtPosition(i);
            serializer.startTag(null,"param");
            serializer.attribute(null, "num_command", com.getStringNumCommand());
            serializer.attribute(null,"num_param",com.getStringNumParam());
            serializer.text(com.getName());
            for (int j=0; j<com.getNumOfValues(); j++){
                View v = com.getValueViewAt(j);
                if(v instanceof EditTextNoKB){
                    serializer.startTag(null, "EditTextNoKB");
                    serializer.text(((EditTextNoKB)v).getText().toString());
                    serializer.endTag(null, "EditTextNoKB");
                }
                else if(v instanceof Spinner){
                    serializer.startTag(null, "Spinner");
                    int selectedPos = ((Spinner) v).getSelectedItemPosition();
                    serializer.attribute(null, "selected_pos", String.valueOf(selectedPos));
                    serializer.text(((Spinner) v).getItemAtPosition(selectedPos).toString());
                    serializer.endTag(null, "Spinner");
                }
            }
            serializer.text(com.getUnity());
            serializer.endTag(null, "param");
        }
        serializer.endTag(null, "list-of-commands");
        serializer.endDocument();

        serializer.flush();

        fos.close();
    }catch (Exception e){
        return false;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

原来这只是一些非常愚蠢的事情。整个问题是我的编码字符串:&#34; UTF-8&#34;。正确的将是&#34; utf-8&#34; (没有资本)。写作和阅读是错误的。我将编辑代码以反映正确的代码。 @OneWorld:谢谢,你是对的,问题出在写作上,然后反映在阅读上。你认为我应该删除这个问题,还是应该把它留在这里作为答案,以便其他人可以从中受益?由于这是一个愚蠢的错误,我不确定我应该保留它。 我是新来的,所以我要求你提示。