为什么我的XML解析器不起作用

时间:2014-06-27 12:33:34

标签: java android xml error-handling xml-parsing

我的资源文件夹中有以下名为test2.xml的XML文件:

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

并尝试使用以下类阅读:

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;
}
}

这个类的部分来自developer.android.com上的示例,我想要理解它,但我似乎没有。啊,它紧挨着

之前的Logtext
if(eventType != XmlPullParser.END_DOCUMENT){
    continue;
}

查询。为什么?我需要做些什么来实际获取带有数据的String? 啊,我还没有编写用于创建HashMap的算法,因为我想知道我是否得到类似{“0”,“0”,“0”,...}或{“0”,“ 0,“,”0,“,...}回来。 如果有人可以帮助我,真的很好。谢谢! 哦,我似乎还没有理解XmlParser,所以如果有更简单的方法,请告诉我:))

1 个答案:

答案 0 :(得分:0)

它因为你没有在解析器中移动而阻塞。你需要调用next()或nextTag()

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

    eventType = parser.next();

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

那应该有帮助