Android XmlPullParser Check IsStartElement(XmlPullParser reader, String name)

Here you can find the source of IsStartElement(XmlPullParser reader, String name)

Description

Is Start Element

License

Open Source License

Declaration

public static boolean IsStartElement(XmlPullParser reader, 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 IsStartElement(XmlPullParser reader)
            throws XmlPullParserException, IOException {
        if (reader == null)
            throw new NullPointerException();
        MoveToContent(reader);/*from w w w  .  jav a2  s . co  m*/
        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));
    }

    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;
    }
}

Related

  1. IsEndElement(XmlPullParser reader)
  2. IsEndElement(XmlPullParser reader, String name)
  3. IsEndElement(XmlPullParser reader, String namespaceName, String localName)
  4. IsName(XmlPullParser reader, String namespaceName, String localName)
  5. IsStartElement(XmlPullParser reader)
  6. IsStartElement(XmlPullParser reader, String namespaceName, String localName)
  7. require(final XmlPullParser pp, final String tagName)
  8. requireAttr(final XmlPullParser pp, final String attrName, final String requiredValue)