Here you can find the source of firstNamedChild(Element el, String lName)
Parameter | Description |
---|---|
el | a parameter |
lName | a parameter |
public static Element firstNamedChild(Element el, String lName)
//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; } }