XmlPullParser返回太多对象

时间:2013-12-10 17:00:35

标签: java android parsing xmlpullparser

我正在尝试解析XML文档并返回一个名为System的对象。相反,对于XML文档中的每个结束标记,都会返回新的System。我试图将我创建系统的位置移动到代码的各个部分,但是System对象只是null,或者会出现关于no return语句的错误。在我尝试移动创建系统的地方之前,这是原始代码:

    private System readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
        //TODO Make it return one system per time through, not multiple after every end tag
        parser.require(XmlPullParser.START_TAG, ns, null);
        String systemName = null;
        String rightAscension = null;
        String declination = null;
        String distance = null;
        while (parser.getEventType() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                Log.i("String", "Start Tag Added");
                continue;
            }
            String name = parser.getName();
            if (name.equalsIgnoreCase("name")) {
                Log.i("Name:", "Name added to parser");
                systemName = readName(parser);
            } else if (name.equalsIgnoreCase("rightascension")) {
                Log.i("RAscen:", "Right Ascension added to parser");
                rightAscension = readRightAscension(parser);
            } else if (name.equalsIgnoreCase("declination")) {
                Log.i("Dec:", "Declination added to parser");
                declination = readDeclination(parser);
            } else if (name.equalsIgnoreCase("distance")) {
                Log.i("Distance:", "Distance added to parser");
                distance = readDistance(parser);
            } else {
                Log.i("Skip: ", "String Skipped");
                skip(parser);
            }
        }
        Log.i("Parser", "New System Added to be displayed");
        return new System(systemName, rightAscension, declination, distance);
    }

如何更改代码,以便在代码末尾只返回一个系统?谢谢!这里有更多我的代码,其中调用了readSystem:

  public List<System> parse(InputStream is) throws XmlPullParserException, IOException {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    Log.i("Parser", "Parser created");
    try {
        //factory = XmlPullParserFactory.newInstance();
       // factory.setNamespaceAware(true);
       // parser = factory.newPullParser();

        parser.setInput(is, null);
        parser.nextTag();
        Log.i("System:", "readSystem called");
        return readSystem(parser);
    } finally {
        is.close();
        Log.i("InputStream", "InputStream Closed");
    }
}
    private List readSystem(XmlPullParser parser) throws XmlPullParserException, IOException {
    List systems = new ArrayList();
        parser.require(XmlPullParser.START_TAG, ns, "system");
        Log.i("Parser", "Passed parser.require");
    String name = parser.getName();
    Log.i("parser", "getName() Called");
    while (parser.next() != XmlPullParser.END_TAG) {
       if (parser.getEventType() != XmlPullParser.START_TAG) {
           Log.i("readSystem", "Start Tag Continue called");
            continue;
       }
        if (name.equalsIgnoreCase("system")) {
            Log.i("String", "System added to parser");
            systems.add(readEntry(parser));
        } else {
           Log.i("String", "No system to add, skipped, no readEntry called");
           skip(parser);
        }

    }
    return systems;
}

1 个答案:

答案 0 :(得分:0)

XML中可能有很多结束标记。例如:

    <foo>
        <bar></bar>
        <baz></baz>
    </foo>

这里我们有3个结束标签。如果我们正在解析foo元素,那么使用当前代码,只要到达bar结束标记,就会停止解析。

当标记是结束标记时,您需要检查节点的名称,并且只有当它是系统标记时才完成解析。

如果整个文档中只有一个“System”元素,您还可以使用XmlPullParser.END_DOCUMENT常量来检查是否已完成解析。你有一个很好的例子:

http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html