get Argument Integer from XML Node - Android XML

Android examples for XML:XML Node

Description

get Argument Integer from XML Node

Demo Code


//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static int getArgInteger(final NodeList args,
            final String strName) {
        final String strValue = getArg(args, strName, null);

        return Integer.valueOf(strValue);
    }/*  w ww. ja  va 2 s .c om*/

    public static String getArg(final Node node, final String strName,
            final String strDefaultValue) {
        return getArg(node.getChildNodes(), strName, strDefaultValue);
    }

    public static String getArg(final NodeList args, final String strName,
            final String strDefaultValue) {
        for (int i = 0; i < args.getLength(); ++i) {
            final Node node = args.item(i);

            if (node instanceof Element) {
                final Element arg = (Element) node;

                if (arg.getAttribute("name").equals(strName)) {
                    return node.getLastChild().getTextContent().trim();
                }
            }
        } // end for

        return strDefaultValue;
    }

    public static double getArg(final Node node, final String strName,
            final double dDefaultValue) {
        return getArg(node.getChildNodes(), strName, dDefaultValue);
    }

    public static double getArg(final NodeList args, final String strName,
            final double dDefaultValue) {
        final String strValue = getArg(args, strName, null);

        if (strValue == null) {
            return dDefaultValue;
        }

        return Double.valueOf(strValue);
    }

    public static int getArg(final Node node, final String strName,
            final int iDefaultValue) {
        return getArg(node.getChildNodes(), strName, iDefaultValue);
    }

    public static int getArg(final NodeList args, final String strName,
            final int iDefaultValue) {
        final String strValue = getArg(args, strName, null);

        if (strValue == null) {
            return iDefaultValue;
        }

        return Integer.valueOf(strValue);
    }
}

Related Tutorials