Why does my XML parser not work

fkarg

I got the following XML file named test2.xml in my assets folder:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="32" height="32" tilewidth="16" tileheight="16">
    <tileset firstgid="1" name="terrain" tilewidth="16" tileheight="16">
    <image source="terrain.png" width="256" height="256"/>
</tileset>
<layer name="Kachelebene 2" width="32" height="32">
    <data encoding="csv">
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,187,187,187,187,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,164,164,164,164,164,164,164,187,187,164,164,187,187,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,164,0,187,187,187,164,164,164,164,187,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,93,94,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,94,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,94,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,92,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    </data>
</layer>
<layer name="Kachelebene 1" width="32" height="32">
    <data encoding="csv">
        3,3,3,164,3,164,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,36,36,36,36,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,36,36,36,3,3,36,36,36,164,36,36,36,36,36,36,36,36,36,36,36,36,3,3,3,3,
        3,3,3,3,3,3,3,3,3,36,36,36,3,3,3,164,164,3,3,3,3,3,3,3,3,3,3,3,36,36,36,3,
        3,3,3,3,3,3,3,3,36,36,3,3,3,187,164,164,164,164,3,3,3,3,3,3,3,3,3,3,3,3,36,36,
        3,3,3,3,3,3,3,36,36,3,3,3,3,3,164,164,187,164,164,164,164,164,3,3,164,164,3,3,3,3,3,3,
        3,3,3,3,3,3,36,36,3,3,3,3,3,3,3,164,164,164,164,164,164,164,164,164,164,3,3,3,3,3,3,3,
        3,3,3,3,3,3,36,3,3,3,3,3,3,3,3,164,3,164,164,164,164,164,164,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,36,36,3,3,3,3,3,3,3,3,164,164,164,164,164,164,164,164,164,164,164,164,3,3,3,3,3,
        3,3,3,3,3,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,3,4,4,4,3,3,3,
        3,3,3,36,36,3,3,3,4,4,4,4,4,4,4,4,4,4,207,207,207,207,207,207,207,3,3,3,3,4,3,3,
        3,3,36,36,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,207,207,207,3,71,3,3,3,4,3,3,
        3,36,36,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,207,3,207,3,71,3,3,3,3,3,3,
        3,36,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,3,3,207,207,3,71,3,3,3,4,4,3,
        36,36,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,3,207,207,207,207,3,71,3,3,3,3,4,3,
        36,3,3,3,3,3,3,3,4,3,71,207,207,207,207,207,207,207,207,207,207,3,207,3,3,71,3,3,3,3,4,3,
        36,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3,
        36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
        36,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        36,3,3,3,3,3,3,3,36,36,36,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        36,3,3,3,3,3,36,36,36,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        36,36,36,36,36,36,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3,
        3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,86,3,3,3,3,3,3,3,3,3,3,3,3,3
    </data>
</layer>
</map>

and tried to read it with the following class:

package de.anlixstudios.davidsdragonadventure;

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

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

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

public class LevelCreator {

public List<HashMap<Integer, List<Integer>>> levels = null;
public List<String> levelName = null;
private GraphicsHolder mGraphicsHolder;
public static final String ns = null;
private GameActivity mGameActivity;
private String heightString;
private String widthString;
private InputStream in_s;
private Context context;
private int height = 0;
private int width = 0;

public LevelCreator(Context context, GraphicsHolder holder){
    // TODO adding parameters for the GameActivity and the GraphicsHolder
    this.context = context;
    mGameActivity = (GameActivity) context;
    mGraphicsHolder = holder;
    Log.d("LevelCreator", "created");
}

// public accessible function, returns the levels
public List<HashMap<Integer, List<Integer>>> getLevels(){
    XmlPullParserFactory pullParserFactory;
    try{
        pullParserFactory = XmlPullParserFactory.newInstance();
        Log.d("pullParserFactory", "new Instance created");
        XmlPullParser parser = pullParserFactory.newPullParser();
        in_s = context.getAssets().open("test2.xml");
        Log.d("got Assets", "setting input and starting");
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in_s, null);
        parser.nextTag();
        return parseXML(parser);
    } catch (XmlPullParserException e){
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    }
    return null;
}

// called from the public function and does the actual work
private List<HashMap<Integer, List<Integer>>> parseXML(XmlPullParser parser) 
throws XmlPullParserException,IOException {

    Log.d("LevelCreator", "parseXML");

    List<HashMap<Integer, List<Integer>>> field = null;

    parser.require(XmlPullParser.START_TAG, ns, "map");

    int eventType = parser.getEventType();

    while(eventType != XmlPullParser.END_DOCUMENT){
        Log.d("LevelCreator", "eventType != XmlPullParser.END_DOCUMENT");
        if(parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        Log.d("LevelCreator", "name: " + name);
        // Starts by looking for the entry tag
        if(name.equals("layer")) {
            Log.d("LevelCreator", "name = layer");
            widthString = (String) parser.getProperty("width");
            heightString = (String) parser.getProperty("height");
            try {
                width = Integer.parseInt(widthString);
                height = Integer.parseInt(heightString);
            } catch(NumberFormatException nfe) {
                System.out.println("Could not parse " + nfe);
            }
            Log.d("width", widthString);
            Log.d("height", heightString);
            field.add(readEntry(parser));
        } else {
            skip(parser);
            Log.d("LevelCreator", "skipped");
        }
    }
    return field;
}

// returns one level int the xml file
private HashMap<Integer, List<Integer>> readEntry(XmlPullParser parser)
throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "layer");
    HashMap<Integer, List<Integer>> data = null;
    while(parser.next() != XmlPullParser.END_TAG) {
        if(parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if(name.equals("data")) {
            Log.d("LevelCreator", "name = data");
            data = readData(parser);
        } else {
            skip(parser);
        }
    }
    return data;
}

private HashMap<Integer, List<Integer>> readData(XmlPullParser parser)
throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "data");
    String dataString = readText(parser);
    Log.d("LevelCreator", "readText");
    parser.require(XmlPullParser.END_TAG, ns, "data");
    // TODO make the String to a HashMap
    return createMap(dataString);
}

// method to skip everything with tags not wanted
private void skip(XmlPullParser parser) throws XmlPullParserException, 
IOException {
            Log.d("LevelCreator", "eventType != XmlPullParser.END_DOCUMENT");
    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;
        }
    }
}

// returns the Text in the <data></data> field
private String readText(XmlPullParser parser) throws IOException, 
XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

// actually returns nothing but should later return a HashMap<Integer, 
// List<Integer>> from a string, with the Info about height and width
private HashMap<Integer, List<Integer>> createMap(String xmldata) {
    Log.d("LevelCreator", "createMap");
    HashMap<Integer, List<Integer>> level;
    List<Integer> row;
    String[] AllData = xmldata.split(",");
    int count = 0;
    for (String data : AllData) {
        Log.d("" + count, data);
    }
    // TODO making the Splitted string to a Hashmap with the width and height

    return null;
}

public List<String> getlevelNames(){
    return levelName;
}
}

Parts of this class are from the example at developer.android.com, I thought to understand it but I seem not to. Ah, and It's stuck at the Logtext right before the

if(eventType != XmlPullParser.END_DOCUMENT){
    continue;
}

query. Why? And what do I have to do to actually get the String with the data? Ah, I haven't written the Algorithm for creating the HashMap yet, because I wanted to see if I get something like { "0", "0", "0", ... } or { "0,", "0,", "0,", ... } back. It would be really nice If anybody could help me. Thx! Oh and I seem not to have understood the XmlParser anyway, so if there is an easier way, please tell me :)

Pentarius

It's blocking because you don't move in the parser. You need to call next() or nextTag()

while(eventType != XmlPullParser.END_DOCUMENT){
    Log.d("LevelCreator", "eventType != XmlPullParser.END_DOCUMENT");

    eventType = parser.next();

    if(eventType != XmlPullParser.START_TAG) {
       continue;
    }
    ...
} 

That should help

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related