get Argument XML Attr - Android XML

Android examples for XML:XML Attribute

Description

get Argument XML Attr

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 String getArgAttr(final NodeList args,
            final String strName, final String strAttrName) {
        for (int i = 0; i < args.getLength(); ++i) {
            final Node n = args.item(i);

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

                if (arg.getAttribute("name").equals(strName)) {
                    return ((Element) n).getAttribute(strAttrName);
                }/*w w w .j  a v a 2s .c  om*/
            }
        } // end for

        return null;
    }

    public static String getArgAttr(final Element node,
            final String strName, final String strAttrName) {
        return getArgAttr(node.getChildNodes(), strName, strAttrName);
    }

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

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

        return Double.parseDouble(strValue);
    }

    public static double getArgAttr(final Element node,
            final String strName, final String strAttrName,
            final double dDefaultValue) {
        final String strValue = getArgAttr(node, strName, strAttrName);

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

        return Double.parseDouble(strValue);
    }

    public static int getArgAttr(final Element node, final String strName,
            final String strAttrName, final int iDefaultValue) {
        final String strValue = getArgAttr(node, strName, strAttrName);

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

        return Integer.parseInt(strValue);
    }

    public static int getArgAttr(final NodeList arg, final String strName,
            final String strAttrName, final int iDefaultValue) {
        final String strValue = getArgAttr(arg, strName, strAttrName);

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

        return Integer.parseInt(strValue);
    }
}

Related Tutorials