Get value from Element : Xml « Development « Android






Get value from Element

     
import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

class Main {

  public static Long getLongElementValue(Element el, String child,
      long defaultValue) {
    try {
      String v = ((Element) el.getElementsByTagName(child).item(0))
          .getChildNodes().item(0).getNodeValue();

      return Long.parseLong(v);
    } catch (Exception ex) {
      return defaultValue;
    }
  }

  public static int getIntElementValue(Element el, String child,
      int defaultValue) {
    try {
      String v = ((Element) el.getElementsByTagName(child).item(0))
          .getChildNodes().item(0).getNodeValue();

      return Integer.parseInt(v);
    } catch (Exception ex) {
      return defaultValue;
    }
  }

  public static ArrayList<Element> getElements(Element parent, String children) {
    NodeList nodelist = parent.getElementsByTagName(children);
    ArrayList<Element> elements = new ArrayList<Element>();
    int l = nodelist.getLength();
    for (int i = 0; i < l; i++) {
      Element element = (Element) nodelist.item(i);
      elements.add(element);
    }
    return elements;
  }

  public static Element getElement(Element parent, String name) {
    NodeList nodelist = parent.getElementsByTagName(name);
    if (nodelist.getLength() > 0) {
      return (Element) nodelist.item(0);
    }
    return null;
  }

}

   
    
    
    
    
  








Related examples in the same category

1.Using xml resource
2.XML Resource Demo
3.Load style from styles.xml
4.Define PreferenceScreen in xml file
5.Using XML Parser
6.XML-defined adapters can be used to easily create adapters in your own application or to pass adapters to other processes.
7.Xml Parse
8.Xml Serializer Uri
9.Get Text Content from Xml Node
10.Escape un escape Xml
11.Get Xml node value with substring
12.Get attribute value
13.Get field from NamedNodeMap
14.get Node Value With Attribute
15.get Character Data From Element
16.update Xml