InputStream from file in internal memory opens incomplete



Why the code below works when I parse a StringReader, but not an InputStream from a file located in the internal memory, even though the file has exactly the same data from the String used before (commented in code)? The parser reaches EOF right after the second line. When I use the String, it goes all the way to the end. The variable linha is just to see, during debugging, what line is being parsed and when I use the InputStream approach, the parser reaches EOF in line 2 of the file.



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


StringReader is = new StringReader("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>\n" +
"<list-of-commands machine=\"1005\" version=\"1.0.0\">\n" +
" <param num_command=\"0\" num_param=\"0\">Velocidade<EditTextNoKB>6</EditTextNoKB></param>\n" +
" <param num_command=\"0\" num_param=\"2\">Sentido<Spinner selected_pos=\"1\">Anti-horĂ¡rio</Spinner></param>\n" +
" <param num_command=\"0\" num_param=\"5\">Zerar Contador</param>\n" +
" <param num_command=\"0\" num_param=\"1\">Desacelerar<EditTextNoKB>69</EditTextNoKB>voltas</param>\n" +
" <param num_command=\"2\" num_param=\"4\">Aguardar Liberar</param>\n" +
"</list-of-commands>");

if(is!=null) {

XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
xmlFactoryObject.setNamespaceAware(true);
myparser.setInput(is,null);
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;

}


The xml file to be parsed is below:





<?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>



No comments:

Post a Comment