Android XmlPullParser Search findChildToDepth(XmlPullParser parser, int parentDepth, String namespaceName, String name)

Here you can find the source of findChildToDepth(XmlPullParser parser, int parentDepth, String namespaceName, String name)

Description

find Child To Depth

License

Open Source License

Declaration

public static boolean findChildToDepth(XmlPullParser parser,
            int parentDepth, 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 findChildToDepth(XmlPullParser parser,
            int parentDepth, String namespaceName, String name)
            throws XmlPullParserException, IOException {
        if (GetDepth(parser) < parentDepth)
            return false;
        while (Read(parser)) {
            int currentDepth = GetDepth(parser);
            if (currentDepth == parentDepth && IsEndElement(parser))
                return false;
            if (currentDepth > parentDepth
                    && IsStartElement(parser, namespaceName, name))
                return true;
        }/*  ww w . ja  v a 2  s. c  om*/
        return false;
    }

    public static int GetDepth(XmlPullParser reader) {
        if (reader == null)
            throw new NullPointerException();
        return Math.max(reader.getDepth() - 1, 0);
    }

    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 IsEndElement(XmlPullParser reader)
            throws XmlPullParserException {
        if (reader == null)
            throw new NullPointerException();
        return IsEndElement(reader, null);
    }

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

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

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

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

Related

  1. matchNameSpaceUri( XmlPullParser paramXmlPullParser, String paramString)
  2. matches(XmlPullParser pp, int type, String namespace, String name)
  3. matches(XmlPullParser pp, int type, String namespace, String name)
  4. test(final XmlPullParser pp, final String tagName)
  5. requireEndDocument(final XmlPullParser pp)