Search in sources :

Example 46 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project android-maps-utils by googlemaps.

the class KmlFeatureParser method createTrack.

/**
 * Creates a new KmlTrack object
 *
 * @return KmlTrack object
 */
private static KmlTrack createTrack(XmlPullParser parser) throws XmlPullParserException, IOException {
    DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault());
    iso8601.setTimeZone(TimeZone.getTimeZone("UTC"));
    ArrayList<LatLng> latLngs = new ArrayList<LatLng>();
    ArrayList<Double> altitudes = new ArrayList<Double>();
    ArrayList<Long> timestamps = new ArrayList<Long>();
    HashMap<String, String> properties = new HashMap<>();
    int eventType = parser.getEventType();
    while (!(eventType == END_TAG && parser.getName().equals("Track"))) {
        if (eventType == START_TAG) {
            if (parser.getName().equals("coord")) {
                String coordinateString = parser.nextText();
                // fields are separated by spaces instead of commas
                LatLngAlt latLngAlt = convertToLatLngAlt(coordinateString, " ");
                latLngs.add(latLngAlt.latLng);
                if (latLngAlt.altitude != null) {
                    altitudes.add(latLngAlt.altitude);
                }
            } else if (parser.getName().equals("when")) {
                try {
                    String dateString = parser.nextText();
                    Date date = iso8601.parse(dateString);
                    long millis = date.getTime();
                    timestamps.add(millis);
                } catch (ParseException e) {
                    throw new XmlPullParserException("Invalid date", parser, e);
                }
            } else if (parser.getName().equals(EXTENDED_DATA)) {
                properties.putAll(setExtendedDataProperties(parser));
            }
        }
        eventType = parser.next();
    }
    return new KmlTrack(latLngs, altitudes, timestamps, properties);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LatLng(com.google.android.gms.maps.model.LatLng) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 47 with START_TAG

use of org.xmlpull.v1.XmlPullParser.START_TAG in project AndroidSDK-RecipeBook by gabu.

the class Recipe016 method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // テストデータ
    String xml = "<user id=\"1\" name=\"gabu\"><age>25</age></user>";
    // パーサのインスタンスを取得
    XmlPullParser parser = Xml.newPullParser();
    try {
        // パーサにXMLをセット
        parser.setInput(new StringReader(xml));
        // イベントタイプを初期化
        int eventType = parser.getEventType();
        // イベントタイプがEND_DOCUMENTになるまでループ
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.END_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    // アトリビュートの数を取得
                    int count = parser.getAttributeCount();
                    // アトリビュートの数だけループ
                    for (int i = 0; i < count; i++) {
                        // アトリビュートの名前を取得
                        String name = parser.getAttributeName(i);
                        Log.d(TAG, "name=" + name);
                        // アトリビュートの値を取得
                        String value = parser.getAttributeValue(i);
                        Log.d(TAG, "value=" + value);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
                case XmlPullParser.TEXT:
                    // START_TAG -> TEXT とイベントが発生する
                    // 今回の例だと<age>25</age>の25の部分が現在の位置
                    // この状態でgetTextメソッドでテキストを取得する
                    Log.d(TAG, "text=" + parser.getText());
                    break;
                default:
                    break;
            }
            // パーサを次のイベントまで進める
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        // when setInput()
        e.printStackTrace();
    } catch (IOException e) {
        // when next()
        e.printStackTrace();
    }
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlPullParserException (org.xmlpull.v1.XmlPullParserException)43 XmlPullParser (org.xmlpull.v1.XmlPullParser)31 IOException (java.io.IOException)26 FileNotFoundException (java.io.FileNotFoundException)24 FileInputStream (java.io.FileInputStream)19 ArrayList (java.util.ArrayList)7 NetworkPolicy (android.net.NetworkPolicy)6 NetworkTemplate (android.net.NetworkTemplate)6 TaskDescription (android.app.ActivityManager.TaskDescription)5 PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 ActivityInfo (android.content.pm.ActivityInfo)5 IPackageInstallerSession (android.content.pm.IPackageInstallerSession)5 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)5 PersistableBundle (android.os.PersistableBundle)5 VolumeRecord (android.os.storage.VolumeRecord)5 ArraySet (android.util.ArraySet)5 AtomicFile (android.util.AtomicFile)5 ReferrerIntent (com.android.internal.content.ReferrerIntent)5 BufferedReader (java.io.BufferedReader)5