Android XmlPullParser Move to Next moveToElement(XmlPullParser parser, String namespaceName, String name)

Here you can find the source of moveToElement(XmlPullParser parser, String namespaceName, String name)

Description

move To Element

License

Open Source License

Declaration

public static boolean moveToElement(XmlPullParser parser,
            String namespaceName, String name)
            throws XmlPullParserException, IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class Main {
    public static boolean moveToElement(XmlPullParser parser,
            String namespaceName, String name)
            throws XmlPullParserException, IOException {
        while (Read(parser)) {
            if (IsStartElement(parser, namespaceName, name))
                return true;
        }/*  w ww.  j a  va 2 s. c o  m*/
        return false;
    }

    public static boolean Read(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        if (parser == null)
            throw new NullPointerException();
        if (parser.getEventType() == XmlPullParser.END_DOCUMENT)
            return false;
        parser.nextToken();
        return true;
    }

    public static boolean IsStartElement(XmlPullParser reader)
            throws XmlPullParserException, IOException {
        if (reader == null)
            throw new NullPointerException();
        MoveToContent(reader);
        return reader.getEventType() == XmlPullParser.START_TAG;
    }

    public static boolean IsStartElement(XmlPullParser reader, String name)
            throws XmlPullParserException, IOException {
        if (reader == null)
            throw new NullPointerException();
        MoveToContent(reader);
        return reader.getEventType() == XmlPullParser.START_TAG
                && (isNullOrEmpty(name) || StringEquals(
                        GetFullName(reader), name));
    }

    public static boolean IsStartElement(XmlPullParser reader,
            String namespaceName, String localName)
            throws XmlPullParserException, IOException {
        if (reader == null)
            throw new NullPointerException();
        MoveToContent(reader);
        return reader.getEventType() == XmlPullParser.START_TAG
                && IsName(reader, namespaceName, localName);
    }

    public static int MoveToContent(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        if (parser == null)
            throw new NullPointerException();
        while (parser.getEventType() == XmlPullParser.PROCESSING_INSTRUCTION
                || parser.getEventType() == XmlPullParser.DOCDECL
                || parser.getEventType() == XmlPullParser.IGNORABLE_WHITESPACE
                || parser.getEventType() == XmlPullParser.START_DOCUMENT) {
            if (!Read(parser)) {
                break;
            }
        }
        return parser.getEventType();
    }

    private static boolean isNullOrEmpty(String s) {
        return s == null || s.length() == 0;
    }

    private static boolean StringEquals(String s1, String s2) {
        if (s1 == s2)
            return true;
        if (s1 == null || s2 == null)
            return false;
        return s1.equals(s2);
    }

    public static String GetFullName(XmlPullParser reader) {
        if (reader.getPrefix() != null)
            return reader.getPrefix() + ":" + reader.getName();
        return reader.getName();
    }

    public static boolean IsName(XmlPullParser reader,
            String namespaceName, String localName) {
        if (reader == null)
            throw new NullPointerException();
        String ns = reader.getNamespace();
        if (isNullOrEmpty(ns)) {
            ns = null;
        }
        if (isNullOrEmpty(namespaceName)) {
            namespaceName = null;
        }
        return (StringEquals(ns, namespaceName))
                && (isNullOrEmpty(localName) || StringEquals(
                        reader.getName(), localName));
    }
}

Related

  1. next(final XmlPullParser pp)
  2. nextDateTime(XmlPullParser parser, String tag)
  3. nextDirectChildTag( XmlPullParser paramXmlPullParser, int paramInt)
  4. nextDouble(XmlPullParser parser, String tag)