List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
/** * get single element by tag name.//from www. j a va 2s . c om * * @param element the element * @return the child elements */ public static List<Element> getChildElements(Element element) { return toElements(element.getChildNodes()); }
From source file:Main.java
/** * Trys to find a child element in the given parent element. * * @param parent The Element to search in. * @param name The name of the element to search for. * * @return The Element if found, null otherwise. */// www . ja v a 2s . c o m public static Element findElement(Element parent, String name) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == n.ELEMENT_NODE) { Element e = (Element) n; if (e.getNodeName().equals(name)) { return e; } } } return null; }
From source file:Main.java
/** * Trys to find a child element in the given parent element where the child's * name attribute is the given value.//from www.j a va2 s . c o m * * @param parent The Element to search in. * @param name The name attribute of the element to search for. * * @return The Element if found, null otherwise. */ public static Element findElementWithNameAttribute(Element parent, String name) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == n.ELEMENT_NODE) { Element e = (Element) n; if (e.getAttribute("name").equals(name)) { return e; } } } return null; }
From source file:Main.java
public static List getChildElements(Element parent, String childName) { NodeList children = parent.getChildNodes(); List list = new ArrayList(); int size = children.getLength(); for (int i = 0; i < size; i++) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (childName.equals(element.getNodeName())) { list.add(element);//w w w . j a v a2 s . c o m } } } return list; }
From source file:XMLUtils.java
/** * Extract all text children of an element *//* w ww .j a v a 2s .c o m*/ public static String extractTextChildren(Element parentNode) { NodeList childNodes = parentNode.getChildNodes(); String result = new String(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) { result += node.getNodeValue(); } } return result; }
From source file:Main.java
/** * Trivial convenience method to extract the value of a (text) Element, coping with the * possibility that text child Nodes may not have been coalesced. * //from ww w . ja v a 2 s .c om * @param textElement */ public static String extractTextElementValue(final Element textElement) { NodeList childNodes = textElement.getChildNodes(); String result; if (childNodes.getLength() == 1) { /* Text Nodes have been coalesced */ result = ensureExtractTextNodeValue(childNodes.item(0)); } else { /* Need to coalesce manually */ StringBuilder resultBuilder = new StringBuilder(); for (int i = 0; i < childNodes.getLength(); i++) { resultBuilder.append(ensureExtractTextNodeValue(childNodes.item(i))); } result = resultBuilder.toString(); } return result; }
From source file:Main.java
/** * @param child//ww w . j a v a 2s.c om * @return the single child element or null * @throws Exception if the child is present multiple times */ public static Element getFirstChild(Element element, String child) throws Exception { NodeList nodes = element.getChildNodes(); Element ret = null; for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) { ret = (Element) childNode; return ret; } } return null; }
From source file:Main.java
public static Element getChildElementByTagNameNS(Element parent, String tagName, String nsName) { NodeList nl = parent.getChildNodes(); int size = nl.getLength(); for (int i = 0; i < size; i++) { Node node = nl.item(i);// w w w . j av a 2 s . co m if (node.getNodeType() == Node.ELEMENT_NODE) { if (tagName.equals(node.getLocalName())) { String ns = node.getNamespaceURI(); if (ns != null && ns.equals(nsName)) { return (Element) node; } } } } return null; }
From source file:Main.java
/** * Retrieve all child elements of the given DOM element that match * the given element getName. Only look at the direct child level of the * given element; do not go into further depth (in contrast to the * DOM API's <code>getElementsByTagName</code> method). * @param ele the DOM element to analyze * @param childEleName the child element getName to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element//from www . ja v a 2s . c o m * @see org.w3c.dom.Element#getElementsByTagName */ public static List<Node> getChildElements(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); List<Node> childEles = new ArrayList<Node>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && nodeNameEquals(node, childEleName)) childEles.add(node); } return childEles; }
From source file:Main.java
public static Object getContent(Element element) { NodeList nl = element.getChildNodes(); StringBuilder content = new StringBuilder(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i);//from ww w. j a v a 2s . co m switch (node.getNodeType()) { case Node.ELEMENT_NODE: return node; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: content.append(node.getNodeValue()); break; } } return content.toString().trim(); }