Android XmlPullParser Read readThisStringArrayXml(XmlPullParser parser, String endTag, String[] name)

Here you can find the source of readThisStringArrayXml(XmlPullParser parser, String endTag, String[] name)

Description

Read a String[] object from an XmlPullParser.

License

Apache License

Parameter

Parameter Description
parser The XmlPullParser from which to read the list data.
endTag Name of the tag that will end the list, usually "string-array".
name An array of one string, used to return the name attribute of the list's tag.

Return

Returns a newly generated String[].

Declaration

public static String[] readThisStringArrayXml(XmlPullParser parser,
        String endTag, String[] name) throws XmlPullParserException,
        java.io.IOException 

Method Source Code

//package com.java2s;
/*/* ww  w.  j  a  v  a 2s  .  co m*/
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Read a String[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeStringArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "string-array".
     * @param name   An array of one string, used to return the name attribute
     *               of the list's tag.
     * @return Returns a newly generated String[].
     * @see #readListXml
     */
    public static String[] readThisStringArrayXml(XmlPullParser parser,
            String endTag, String[] name) throws XmlPullParserException,
            java.io.IOException {

        int num;
        try {
            num = Integer.parseInt(parser.getAttributeValue(null, "num"));
        } catch (NullPointerException e) {
            throw new XmlPullParserException(
                    "Need num attribute in string-array");
        } catch (NumberFormatException e) {
            throw new XmlPullParserException(
                    "Not a number in num attribute in string-array");
        }
        parser.next();

        String[] array = new String[num];
        int i = 0;

        int eventType = parser.getEventType();
        do {
            if (eventType == XmlPullParser.START_TAG) {
                if (parser.getName().equals("item")) {
                    try {
                        array[i] = parser.getAttributeValue(null, "value");
                    } catch (NullPointerException e) {
                        throw new XmlPullParserException(
                                "Need value attribute in item");
                    } catch (NumberFormatException e) {
                        throw new XmlPullParserException(
                                "Not a number in value attribute in item");
                    }
                } else {
                    throw new XmlPullParserException(
                            "Expected item tag at: " + parser.getName());
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (parser.getName().equals(endTag)) {
                    return array;
                } else if (parser.getName().equals("item")) {
                    i++;
                } else {
                    throw new XmlPullParserException("Expected " + endTag
                            + " end tag at: " + parser.getName());
                }
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.END_DOCUMENT);

        throw new XmlPullParserException("Document ended before " + endTag
                + " end tag");
    }
}

Related

  1. readThisSetXml(XmlPullParser parser, String endTag, String[] name)
  2. readThisSetXml(XmlPullParser parser, String endTag, String[] name)
  3. readThisSetXml(XmlPullParser parser, String endTag, String[] name)
  4. readThisSetXml(XmlPullParser parser, String endTag, String[] name)
  5. readThisStringArrayXml( XmlPullParser parser, String endTag, String[] name)
  6. readThisValueXml(XmlPullParser parser, String[] name)
  7. readThisValueXml(XmlPullParser parser, String[] name)
  8. readThisValueXml(XmlPullParser parser, String[] name)
  9. readThisValueXml(XmlPullParser parser, String[] name)