get XML Element Contents As Integer - Android XML

Android examples for XML:XML Element

Description

get XML Element Contents As Integer

Demo Code

/*//from   w  w w  .  j a va2 s .  c  o  m
 * XmlUtil.java
 *
 * (c) 2009  The Echo Nest
 * See "license.txt" for terms
 *
 *
 */
//package com.java2s;
import java.io.IOException;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static int getElementContentsAsInteger(Element element,
            String elementName) throws IOException {
        int results = 0;
        Element first = getFirstElement(element, elementName);
        if (first != null) {
            try {
                results = Integer.parseInt(first.getTextContent());
            } catch (NumberFormatException ex) {
            }
        }
        return results;
    }

    public static Element getFirstElement(Element element,
            String elementName) throws IOException {
        NodeList list = element.getElementsByTagName(elementName);
        if (list.getLength() >= 1) {
            Element subElement = (Element) list.item(0);
            return subElement;
        } else {
            return null;
        }
    }
}

Related Tutorials