List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Utils.java
/** * <p>Returns the first child element with the given name. Returns * <code>null</code> if not found.</p> * * @param parent parent element/*from www . j a v a 2 s . c om*/ * @param name name of the child element * @return child element */ public static Element getChildElementByName(Element parent, String name) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.getTagName().equals(name)) { return element; } } } return null; }
From source file:Main.java
public static String getElementValue(Element parent) { NodeList nodes = parent.getChildNodes(); int current = 0; int length = nodes.getLength(); while (current < length) { Node node = nodes.item(current); if (node instanceof Text) { String value = node.getNodeValue(); if (value != null) { return value.trim(); }//from w ww . j a v a 2s. c om } current++; } return ""; }
From source file:Main.java
public static List<String> getChildListValuesByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); List<String> childEles = new ArrayList<String>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i);// w w w . j a v a 2 s . c o m if (node instanceof Element && childEleName.equals(node.getNodeName()) || childEleName.equals(node.getLocalName())) { childEles.add(getTextValue((Element) node)); } } return childEles; }
From source file:Main.java
public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) { NodeList nl = e.getChildNodes(); Element result = null;//from w w w . j ava 2s. c om for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (result != null) // more than one Element children throw exc; result = (Element) n; } } if (result == null // zero Element children || !result.getTagName().equals(tag)) // wrong tag throw exc; return result; }
From source file:Main.java
public static void getInnerText(StringBuilder sb, Element elt, String separator) { NodeList tns = elt.getChildNodes(); for (int j = 0; j < tns.getLength(); j++) { Node tn = tns.item(j);/*from www. j av a 2 s. c o m*/ if (tn.getNodeType() == Node.TEXT_NODE) { sb.append(tn.getNodeValue()); } else if (tn.getNodeType() == Node.ELEMENT_NODE) { sb.append(separator); getInnerText(sb, (Element) tn, separator); sb.append(separator); } } }
From source file:Main.java
/** * returns the text value associated with the element * * @param target - the element//from w ww .java2 s . co m * @return - the text value */ public static String getElementValue(final Element target) { final NodeList nodeList = target.getChildNodes(); if (nodeList == null) { return null; } for (int current = 0; current < nodeList.getLength(); current++) { final Node node = nodeList.item(current); if (node instanceof Text) { final Text text = (Text) node; final String value = text.getNodeValue(); if ((value != null) && (value.length() > 0)) { return value; } } } return ""; }
From source file:Main.java
/** * Gets the first child element.//from www .j a va2 s . c o m * * @param e the element. * @return the first child element. */ public static Element getFirstChildElement(Element e) { if (e != null) { NodeList children = e.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { return (Element) node; } } } return null; }
From source file:Main.java
public static List<Element> getChildElementsByTagName(Element parentElement, String childTag) { NodeList nodelist = parentElement.getChildNodes(); List<Element> nodes = new ArrayList<Element>(); for (int i = 0; i < nodelist.getLength(); i++) { Node temp = nodelist.item(i); if (temp.getNodeType() == Node.ELEMENT_NODE && temp.getNodeName().equals(childTag)) { nodes.add((Element) temp); }//w w w . j a v a 2 s .c om } return nodes; }
From source file:Main.java
public static void genericRemoveAll(Element parent, String tagname) { NodeList nl = parent.getChildNodes(); List<Element> parts = new ArrayList<Element>(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof Element) { Element elem = (Element) nl.item(i); if (elem.getTagName().equals(tagname)) parts.add(elem);/* w w w.j a va 2 s . c o m*/ } } for (Element part : parts) parent.removeChild(part); }
From source file:Main.java
public static String getNodeText(Node parentNode, String nodeName) { Element fstElmnt = (Element) parentNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName); Element fstNmElmnt = (Element) fstNmElmntLst.item(0); NodeList fstNm = fstNmElmnt.getChildNodes(); String ret = fstNm.item(0).getNodeValue(); return ret;//from www .ja v a 2 s. c om }