Java XML First Child Element firstNamedChild(Element el, String lName)

Here you can find the source of firstNamedChild(Element el, String lName)

Description

return the first child of the element with given name, or null if there are none

License

Open Source License

Parameter

Parameter Description
el a parameter
lName a parameter

Declaration

public static Element firstNamedChild(Element el, String lName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;
import java.util.Vector;
import java.util.StringTokenizer;

import org.w3c.dom.Element;

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

public class Main {
    /**/*from  w w w  . j a v a  2s  .c o m*/
     * return the first child of the element with given name, or null if there are none
     * @param el
     * @param lName
     * @return
     */
    public static Element firstNamedChild(Element el, String lName) {
        if (el == null)
            return null;
        Element fc = null;
        if (namedChildElements(el, lName).size() > 0)
            fc = namedChildElements(el, lName).elementAt(0);
        return fc;
    }

    /**
     * return the Vector of child Elements with given local name
     * @param el
     * @param name
     * @return
     */
    public static Vector<Element> namedChildElements(Element el, String lName) {
        Vector<Element> nc = new Vector<Element>();
        for (Iterator<Element> it = childElements(el).iterator(); it.hasNext();) {
            Element en = it.next();
            if (getLocalName(en).equals(lName))
                nc.addElement(en);
        }
        return nc;
    }

    /**
     * Vector of child elements of an element
     */
    public static Vector<Element> childElements(Element el) {
        Vector<Element> res = new Vector<Element>();
        NodeList nodes = el.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node nd = nodes.item(i);
            if (nd instanceof Element) {
                Element eg = (Element) nd;
                res.addElement(eg);
            }
        }
        return res;
    }

    /**
     * get the name of an XML element, with the namespace prefix stripped off
     * @param el
     * @return
     */
    public static String getLocalName(Node el) {
        String locName = "";
        StringTokenizer st = new StringTokenizer(el.getNodeName(), ":");
        while (st.hasMoreTokens())
            locName = st.nextToken();
        return locName;
    }
}

Related

  1. firstChildElement(Node node)
  2. firstChildElement(Node node)
  3. firstChildNodeWithName(org.w3c.dom.Node node, String name)
  4. firstChildTextContent(Element element, String name)
  5. firstElementNodeChild(Node n)
  6. firstTextChild(Element el)
  7. getDirectChild(Node fNode, String localName, String namespace)
  8. getDirectChild(Node fNode, String localName, String namespace)
  9. getDirectChildElement(Element p_rootElement, String p_elementName)