Android XML Element Child Get valueOfFirstChildWithName(Element element, String name)

Here you can find the source of valueOfFirstChildWithName(Element element, String name)

Description

value Of First Child With Name

License

Open Source License

Declaration

public static String valueOfFirstChildWithName(Element element,
            String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Jose Alcal? Correa.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * /*from w  w  w  . ja v  a 2 s . c om*/
 * Contributors:
 *     Jose Alcal? Correa - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static String valueOfFirstChildWithName(Element element,
            String name) {
        Element e = firstChildWithName(element, name);
        if (e != null) {
            Node firstChild = e.getFirstChild();
            if (firstChild != null)
                return firstChild.getNodeValue();
        }

        return null;
    }

    public static Element firstChildWithName(Element element, String name) {
        NodeList list = element.getChildNodes();
        final int length = list.getLength();
        for (int i = 0; i < length; ++i) {
            Node n = list.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && n.getNodeName().equals(name))
                return (Element) n;
        }

        return null;
    }
}

Related

  1. getChildByType(Element element, short nodeType)
  2. getChildElementByName(Element parent, String name)
  3. getChildValue(Element element, String name)
  4. getFullTextChildrenFromElement(Element element)
  5. printChildElements(Element root, PrintStream out, boolean recurse, String prefix)
  6. attributeOfFirstChildWithName(Element element, String name, String attribute)
  7. childrenWithName(Element e, String name)
  8. getCDataNode(Element element)
  9. locateElements(Element root, String tagName, String keyAttributeName, String keyAttributeValue)