循环XML解析器,直到标记完成 [英] Looping an xml parser till a tag is done

查看:173
本文介绍了循环XML解析器,直到标记完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个XML文件中提取信息,这是code这我目前正在开发,使其工作。

I'm trying to pull information from a xml file and this is the code which I'm currently developing to make it work.

private void parseXML(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    int eventType = parser.getEventType();

    Map<String, Map<String, Double>> sports = new HashMap<String, Map<String, Double>>();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        String name = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String sportName = null;
            boolean paid = false;
            Map<String, Double> preset = null;

            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    preset = new HashMap<String, Double>();
                    break;
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equals("sport")) {
                        sportName = parser.getAttributeValue(0);
                        paid = Boolean.parseBoolean(parser
                                .getAttributeValue(1));
                    } else if (name.equals("preset")) {
                        // TODO figure out how to loop this so it finished up the sport and then moves on to the next one
                        String presetName = parser
                                .getAttributeValue(0);
                        double value = Double.parseDouble(parser
                                .nextText());
                        preset.put(presetName, value);
                        parser.next();
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
            }
            sports.put(sportName, preset);
            eventType = parser.next();
        }
    }
}

这是其甩开

<?xml version="1.0" encoding="utf-8"?>
<sports>
    <sport name="Baseball" paid="false">
        <preset name="Pitching Mound">726.0</preset>
        <preset name="Base Distance">1080.0</preset>
    </sport>
    <sport name="Basketball" paid="false">
        <preset name="NBA Free Throw Line">181.08</preset>
        <preset name="NBA 3pt Line">265.8</preset>
    </sport>
    <sport name="Cricket" paid="true">
        <preset name="Cricket Pitch">2012.0</preset>
    </sport>
</sports>

什么是code本质上是做越来越这项运动的名称,无论是付费功能,或者不,我已经为它定义的preset中。我正在试图做的是让部分,它增加了preSET到preSET地图,循环,直到运动$ P $的pset的结束。我不舒服的使用XML解析,因为我会混淆使用它100%。我试图获取属性计数时,code得到这项运动的名称,然后循环,但它没有工作。

What the code essentially does is getting the sport name, whether it is a paid feature or not, and the presets I have defined for it. What I'm trying to do is make the part, where it adds a preset to the preset map, loop till the end of the sports presets. I'm not 100% comfortable with using the xml parses because I get confused with using it. I tried getting the attribute count when the code gets the name of the sport, and then looping it but it didnt work.

推荐答案

而Android文档称其推荐使用XmlPullParser你可以使用你选择的解析器。

While the android docs saying its recommended to use XmlPullParser you can use parser of your choice.

要获取属性可以使用 parser.getAttributeValue(NULL,名)因为你有&LT; preSET NAME =投球土墩&GT; 。名称是属性。

To get attribute you can use parser.getAttributeValue(null, "name") coz you have <preset name="Pitching Mound">. Name is the attribute.

要获取文本使用文本

有在文档为例。它具有为每个标签单独的方法,而方法是相同的。

There is a example in the docs. It has separate methods for each tag while the method is the same.

http://developer.android.com/training/basics/网络-OPS / xml.html

您可以使用下面的参考。

You can use the below for reference.

public class XMLPullParserHandler {

    private String text;

    public XMLPullParserHandler() {

    }



    public Void parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();

            parser.setInput(is, null);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("sports")) {

                    }
                    if (tagname.equalsIgnoreCase("preset")) {
                        Log.i(".......","Attribute value is"+parser.getAttributeValue(null, "name"));
                    }


                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("sports")) {
                        // add employee object to list

                    } else if (tagname.equalsIgnoreCase("sport")) {
                         // no value

                    } 
                    else if (tagname.equalsIgnoreCase("preset")) {
                       Log.i("Preset is",text);

                    } 
                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

日志输出

03-25 02:03:00.412: I/Attribute value is(1119):  Pitching Mound
03-25 02:03:00.412: I/Preset is(1119): 726.0
03-25 02:03:00.412: I/Attribute value is(1119):  Base Distance
03-25 02:03:00.422: I/Preset is(1119): 1080.0
03-25 02:03:00.422: I/Attribute value is(1119):  NBA Free Throw Line
03-25 02:03:00.422: I/Preset is(1119): 181.08
03-25 02:03:00.422: I/Attribute value is(1119):  NBA 3pt Line
03-25 02:03:00.422: I/Preset is(1119): 265.8
03-25 02:03:00.422: I/Attribute value is(1119):  Cricket Pitch
03-25 02:03:00.422: I/Preset is(1119): 2012.0

编辑:

要获得情况下XmlPullParser.START_TAG支付只需添加以下属性:

 else if (tagname.equalsIgnoreCase("sport")) {
          Log.i("Attribute value is"," "+parser.getAttributeValue(null, "paid"));
 }

这篇关于循环XML解析器,直到标记完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆