-1

everyone, I'm new in Android and I'm trying to parse a XML file and get the info in a List. My XML file is this and what I want is get the objects.

What I am doing to parse it is something similar as the example on the android developeres website:

import android.content.Context;
import android.util.Xml;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Álvaro on 31/08/2015.
 */
public class EntrelazadasXMLParser {
    // We don't use namespaces
    private static final String ns = null;

public List parse(InputStream in, Context context) throws /*XmlPullParserException,*/ IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readFeed(parser,context);

    }catch (XmlPullParserException e){

        Toast.makeText(context, "Error en el parser: " + e.toString(),
                Toast.LENGTH_LONG).show();
        return null;
    }
    finally {
        in.close();
    }

}
    ....

This steo is to instantiate a parser and start the parsing process. The parser is initialized Without namespaces and to use the provided InputStream as its input.It starts the parsing process with a call to nextTag() and invokes the readFeed() method, which extracts and processes the data the app is interested in. Here is where the exception happens and the error is the following.

Error en el parser: org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:1 in java.io.InputStreamReader@41e3d220).

The rest of this class s this:

vate List readFeed(XmlPullParser parser, Context context) throws /*XmlPullParserException,*/ IOException {
    List entries = new ArrayList();
    try {
        parser.require(XmlPullParser.START_TAG, ns, "database");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the table tag
            if (name.equals("table")) {
                entries.add(readEntry(parser));
            } else {
                skip(parser);
            }
        }
        return entries;
    }
    catch (XmlPullParserException e){

        Toast.makeText(context, "Error en el read feed: " + e.toString(),
                Toast.LENGTH_LONG).show();
        return null;
    }
}

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
            case XmlPullParser.END_TAG:
                depth--;
                break;
            case XmlPullParser.START_TAG:
                depth++;
                break;
        }
    }
}

private Producto readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "table");

    Producto Objeto =null;
    String summary = null;
    String link = null;

    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("column")) {
            Objeto = readColumn(parser);
        } else {
            skip(parser);

        }
    }
    return Objeto;
}

private Producto readColumn(XmlPullParser parser) throws IOException, XmlPullParserException {
    Producto Objeto ;
    String Id = null;;
    String Referencia = null;;
    String Nombre = null;;
    String Categoria = null;;
    String SubFamilia = null;;
    String Familia = null;;
    String Descripcion = null;;
    String Precio = null;;
    String Image1 = null;
    String Image2 = null;
    String Image3 = null;
    String Image4 = null;
    String Image5 = null;
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String tag = parser.getName();
    String relType = parser.getAttributeValue(null, "name");

        if (relType.equals("Id")){
            Id = readId(parser);
        } else if(relType.equals("Precio")){
            Precio = readPrecio(parser);
        }else if(relType.equals("Referencia")){
            Referencia = readReferencia(parser);
        }else if(relType.equals("Categoria")){
            Categoria = readCategoria(parser);
        }else if(relType.equals("Nombre")){
            Nombre = readNombre(parser);
        }else if(relType.equals("Subfamilia")){
            SubFamilia = readSubfamilia(parser);
        }else if(relType.equals("Familia")){
            Familia = readFamilia(parser);
        }else if(relType.equals("Descripcion")){
            Descripcion = readDescripcion(parser);
        }else{
            Image1 = readImage(parser);
        }

    Objeto =new Producto(Id,Referencia,Nombre,Categoria,SubFamilia,Familia,Descripcion,Precio,Image1,Image2,Image3,Image4,Image5);

    parser.require(XmlPullParser.END_TAG, ns, "column");
    return Objeto;
}

// Processes Id tags in the feed.
private String readId (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readPrecio (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readReferencia (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readCategoria (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readNombre (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readSubfamilia (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readFamilia (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readDescripcion (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}

private String readImage (XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "column");
    String value = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "column");
    return value;
}
// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

}

And the call to the parser and the declaration of the inputStream:

EntrelazadasXMLParser XmlParser = new EntrelazadasXMLParser();
    List<Producto> entries = null;
    String root = Environment.getExternalStorageDirectory().toString();
    File SDCardRoot = new File(root + "/Entrelazadas");

    try {

        InputStream raw = new FileInputStream(new File(SDCardRoot, "catalogo.xml"));

        //FileInputStream fileInputStream = new FileInputStream(file);
        entries = XmlParser.parse(raw,context);

    }
    catch (FileNotFoundException e){
        Toast.makeText(context, "Fichero no encontrado: " +e.toString(),
                Toast.LENGTH_LONG).show();
    }
    catch (IOException e){
        Toast.makeText(context, "Error de IO: " +e.toString(),
                Toast.LENGTH_LONG).show();
    }

I hope any of you can help me. Thanks a lot.


This is how the file appears in its very first lines:

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Álvaro
  • 1
  • 3
  • 1
    What happens if you remove this line ``? – Phantômaxx Sep 01 '15 at 10:05
  • which line @FrankN.Stein? – Álvaro Sep 01 '15 at 10:10
  • The second one: `` – Phantômaxx Sep 01 '15 at 10:14
  • sorry but i don't find that line in my code or xml file. @FrankN.Stein – Álvaro Sep 01 '15 at 10:43
  • So the link you posted points to some other file? I can read (first 3 lines): ` ` – Phantômaxx Sep 01 '15 at 10:45
  • Are you already drunk, at this time in the morning? ;) – Phantômaxx Sep 01 '15 at 10:45
  • It doesn't appears in the file, I check it , probably its somethig with the browser. This line is not in the XML file. No man I'm not drunk, and you?? @FrankN.Stein – Álvaro Sep 01 '15 at 10:55
  • No, unfortunately... ;) But, please, take a moment to click your link and then tell me. – Phantômaxx Sep 01 '15 at 10:56
  • Really man, I check the file, i'm not joking it is for a project and i want to fix this problem, and if you want i can send you a screenshot, but that line is not in the XML file. If it will be there I try to erase and check if it works properlly without that line. @FrankN.Stein – Álvaro Sep 01 '15 at 11:02
  • Really, man. I edited your question to show it to you. It's a screenshot of what appears when I click your link. – Phantômaxx Sep 01 '15 at 11:09
  • And, apart the rest, your file seems to be missing an important line: the xml definition. I.e.: `` – Phantômaxx Sep 01 '15 at 11:11
  • Ok, as i said probably is something with the browser xml reader or other thing but i check the file and the line is not there, and yes the line that you said is in the file but the browser don't show it. I think it could be the problem but in the file that i download in the mobile phone this line is writed. – Álvaro Sep 01 '15 at 11:34

2 Answers2

0

Iterate while loop until end of document reach like this

private void eBooksXmlParser(String xmlData, Context context) {
    XmlPullParserFactory pullParserFactory;
    ArrayList<EBook> eBooks = null;
    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(new StringReader(xmlData));

        int eventType = parser.getEventType();
        EBook currentEBook = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagName;
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    tagName = parser.getName();
                    if (tagName.equals("wr"))
                        eBooks = new ArrayList<>();
                    else if (eBooks != null) {
                        if (tagName.equals("book")) {
                            currentEBook = new EBook();
                            currentEBook.setBookName(parser.getAttributeValue(0));
                            currentEBook.setCategory(parser.getAttributeValue(1));
                            currentEBook.setDescription(parser.getAttributeValue(2));
                            currentEBook.setFileName(parser.getAttributeValue(3));
                            currentEBook.setFormat(parser.getAttributeValue(4));
                            currentEBook.setFromDate(parser.getAttributeValue(5));
                            currentEBook.setSubCategory(parser.getAttributeValue(6));
                            currentEBook.setThumbnail(parser.getAttributeValue(7));
                            currentEBook.setTier(parser.getAttributeValue(8));
                            currentEBook.setToDate(parser.getAttributeValue(9));
                            currentEBook.setType(parser.getAttributeValue(10));
                            currentEBook.setDownloadStatus(false);
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    tagName = parser.getName();
                    if (tagName.equalsIgnoreCase("book")
                            && currentEBook != null) {
                        eBooks.add(currentEBook);
                        currentEBook = null;
                    }
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
    if (eBooks != null)
        UserDbProvider.getInstance(context).addEBooksList(
                eBooks);
}
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
Rashmi Jain
  • 343
  • 2
  • 10
0

The problem finally was the file, which was empty due to I had a line in other part of the code that erase that file.

Álvaro
  • 1
  • 3