用xmlpullparser

时间:2017-08-17 07:22:43

标签: android xml

我是编程的新手,为了更好地提高我对android中解析xml的理解,我试图从网络服务中获取有关地震的一些数据。我已经在Stackoverflow和不同的教程中阅读了所有相关主题,但是我的代码中出现了问题。 这是我要解析的Xml的摘录:

<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<q:quakeml xmlns:q="http://quakeml.org/xmlns/quakeml/1.2" xmlns="http://quakeml.org/xmlns/bed/1.2" xmlns:ingv="http://webservices.ingv.it/fdsnws/event/1">
    <eventParameters publicID="smi:webservices.ingv.it/fdsnws/event/1/query">    
        <event publicID="smi:webservices.ingv.it/fdsnws/event/1/query?eventId=16736781">      
            <type>earthquake</type>      
            <description>        
                <type>region name</type>        
                <text>Tirreno Meridionale (MARE)</text>      
            </description>      
            <preferredMagnitudeID>smi:webservices.ingv.it/fdsnws/event/1/query?magnitudeId=49992691</preferredMagnitudeID>      
            <preferredOriginID>smi:webservices.ingv.it/fdsnws/event/1/query?originId=49089691</preferredOriginID>      
            <creationInfo>        
                <agencyID>INGV</agencyID>        
                <author>SURVEY-INGV</author>        
                <creationTime>2017-08-16T14:59:48</creationTime>      
            </creationInfo>

以下是我写的代码部分:

private static List<Earthquake> extractFeatureFromQuakeml(String earthquakeXML) throws XmlPullParserException, IOException {

    XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
    xmlFactoryObject.setNamespaceAware(true);
    XmlPullParser myparser = xmlFactoryObject.newPullParser();

    myparser.setInput(new StringReader(earthquakeXML));

    // If the JSON string is empty or null, then return early.
    if (TextUtils.isEmpty(earthquakeXML)) {
        return null;
    }

    // Create an empty ArrayList that we can start adding earthquakes to
    List<Earthquake> earthquakes = new ArrayList<>();

    try {
        int eventType = myparser.getEventType();
        Log.d("Step", "Started parsing");

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String location = "";
            String time = "";
            double magnitude = 0.0;
            String url = "";
            String tag = myparser.getName();
            String text = "";

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    //Log.d("Step","Started parsing, end tag: " + tag);
                    if (tag.equalsIgnoreCase("event")) {
                        url = myparser.getAttributeValue(null, "publicID");
                        Log.d(LOG_TAG, "Url" + url);
                        myparser.next();
                    }

                    break;

                case XmlPullParser.END_TAG:
                    text = myparser.getText();
                    if (text != null) {
                        if (tag.equalsIgnoreCase("type") && text.equalsIgnoreCase("region name")) {
                            myparser.nextText();
                            location = "" + myparser.getText();
                            Log.d(LOG_TAG, "Location" + location);
                        }
                    }

                    if (tag.equalsIgnoreCase("creationTime")) {
                        time = "" + myparser.getText();
                        Log.d(LOG_TAG, "Time" + time);
                    }

                    if (tag.equalsIgnoreCase("mag")) {
                        myparser.next();
                        myparser.next();
                        String smagnitude = myparser.getText();
                        Log.d(LOG_TAG, "Magnitude" + smagnitude);
                    }

                    if (tag.equalsIgnoreCase("creationTime")) {
                        time = "" + myparser.getText();
                        Log.d(LOG_TAG, "Time" + time);
                    }

                    if (tag.equalsIgnoreCase("mag")) {
                        myparser.next();
                        myparser.next();
                        String smagnitude = myparser.getText();
                        Log.d(LOG_TAG, "Magnitude" + smagnitude);
                    }

                    break;
            }

            // Create a new {@link Earthquake} object with the magnitude, location, time,
            // and url from the JSON response.
            if (location != null && magnitude != 0.0 && time != null && url != null) {
                Earthquake earthquake = new Earthquake(magnitude, location, time, url);
                // Add the new {@link Earthquake} to the list of earthquakes.
                earthquakes.add(earthquake);
            }

            eventType = myparser.next();
        }

    } catch (XmlPullParserException e) {

        Log.e("QueryUtils", "Problem parsing the earthquake XML results", e);
    }

    // Return the list of earthquakes
    return earthquakes;
}

现在我得到的唯一值是url,其他变量总是为null。 有人可以指出我的代码中的缺陷吗?请保持温和,我在3周前开始编码:)

非常感谢你!

1 个答案:

答案 0 :(得分:0)

你的方法的问题是你在while循环中每次都是空的,你应该理解while循环会多次调用(例如,当你获得Url并将该值存储在url中时,你正在调用myparser.next();所以它将获得下一个元素type并且它将在循环中进入而你现在要删除每个值,你也正在删除url值。)。

相反,当get start标记为Earthquake时,您可以创建event对象,并且每个结束标记都可以在Earthquake对象和{{的结尾标记处添加coressponding值1}},将event对象添加到列表中。

我修改了你的代码,怎么做,你可以设置所有剩余的值

Earthquake