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
public static String getElementText(Element e) { if (e == null) { return (""); }/*from w w w . j a va2 s .c om*/ NodeList children = e.getChildNodes(); if (children == null) { return (""); } StringBuffer text = new StringBuffer(); int listLength = children.getLength(); for (int i = 0; i < listLength; i++) { Node node = children.item(i); int nodeType = node.getNodeType(); if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) { String nodeValue = node.getNodeValue(); if (nodeValue != null) { text.append(nodeValue); } } } return (text.toString().trim()); }
From source file:Main.java
static List<Element> findChildren(Element element, String ns, String tag) { final List<Element> list = new ArrayList<Element>(); for (Node node : listOf(element.getChildNodes())) { if (tag.equals(node.getLocalName()) && ((ns == null) || node.getNamespaceURI().equals(ns))) { list.add((Element) node); }//from w w w . j av a2 s .co m } return list; }
From source file:XMLUtils.java
/** * Returns the value of the child node with the given name. * @param base the element from where to search. * @param name of the element to get.//from w ww . j a va 2 s . c om * @return the value of this element. */ public static String getChildStringValueForElement(final Element base, final String name) { String value = null; NodeList nodeList = base.getChildNodes(); if (nodeList.getLength() > 0) { int length = nodeList.getLength(); for (int i = 0; i < length; i++) { Node node = nodeList.item(i); // Get an element, create an instance of the element if (Node.ELEMENT_NODE == node.getNodeType()) { if (name.equals(node.getNodeName())) { // Get value of this child Node elNode = ((Element) node).getFirstChild(); if (elNode != null) { value = elNode.getNodeValue(); break; } } } } } return value; }
From source file:Main.java
/** * Takes a number of tags of name 'name' that are children of 'root', and * looks for attributes of 'attrib' on them. Converts attributes to an * int and returns in an array.// w w w. j a va 2 s. c o m */ public static String[] getElementArrayString(Element root, String name, String attrib) { if (root == null) return null; NodeList nl = root.getChildNodes(); LinkedList<String> elementCache = new LinkedList<String>(); int size = nl.getLength(); for (int i = 0; i < size; i++) { Node node = nl.item(i); if (!(node instanceof Element)) continue; Element ele = (Element) node; if (!ele.getTagName().equals(name)) continue; String valS = ele.getAttribute(attrib); elementCache.addLast(valS); } String[] retArr = new String[elementCache.size()]; Iterator<String> it = elementCache.iterator(); int idx = 0; while (it.hasNext()) { retArr[idx++] = it.next(); } return retArr; }
From source file:Main.java
/** * Returns the child elements with the specified name for the specified element. * * @param element the parent element/*ww w .jav a 2 s . c om*/ * @param name the name of the child elements to return * * @return the child elements with the specified name for the specified element */ public static List<Element> getChildElements(Element element, String name) { List<Element> childElements = new ArrayList<>(); NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { Element childElement = (Element) node; if (childElement.getNodeName().equals(name)) { childElements.add(childElement); } } } return childElements; }
From source file:DomUtils.java
/** * Utility method that returns the first child element * identified by its name./* w w w . j a va 2 s.c o m*/ * @param ele the DOM element to analyze * @param childEleName the child element name to look for * @return the <code>org.w3c.dom.Element</code> instance, * or <code>null</code> if none found */ public static Element getChildElementByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && nodeNameEquals(node, childEleName)) { return (Element) node; } } return null; }
From source file:Main.java
/** * Get the child elements having the supplied localname and namespace. * Can be used instead of XPath.// w w w . j a v a2 s. com * @param parent Parent element to be searched. * @param localname Localname of the element required. Supports "*" wildcards. * @param namespaceURI Namespace URI of the required element, or null * if a namespace comparison is not to be performed. * @return A list of W3C DOM {@link Element}s. An empty list if no such * child elements exist on the parent element. */ public static List<Element> getElements(Element parent, String localname, String namespaceURI) { return getElements(parent.getChildNodes(), localname, namespaceURI); }
From source file:DomUtils.java
/** * Retrieve all child elements of the given DOM element that match * the given element name. 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 name to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element/*from w w w .ja v a 2 s.co m*/ * @see org.w3c.dom.Element#getElementsByTagName */ public static List getChildElementsByTagName(Element ele, String childEleName) { NodeList nl = ele.getChildNodes(); List childEles = new ArrayList(); 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
/** * Returns the child element with the specified tagName for the specified parent element * /*from ww w .j a v a 2 s .co m*/ * @param parent * @param tagName * @return */ public static Element getChildElementByTagName(Element parent, String tagName) { if (parent == null || tagName == null) { return null; } NodeList nodes = parent.getChildNodes(); Node node; int len = nodes.getLength(); for (int i = 0; i < len; i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && ((Element) node).getNodeName().equals(tagName)) { return (Element) node; } } return null; }
From source file:Main.java
/** * Access all immediate child elements inside the given Element * * @param element the starting element, cannot be null. * @param namespaceURI name space of the child element to look for, * cannot be null. Use "*" to match all namespaces * @param elemName the name of the child element to look for, * cannot be null.//w ww . jav a2 s .co m * @return array of all immediate child element inside element, or * an array of size zero if no child elements are found. */ public static Element[] getChildElements(Element element, String namespaceURI, String elemName) { NodeList list = element.getChildNodes(); int len = list.getLength(); ArrayList<Node> array = new ArrayList<Node>(len); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (elemName.equals(n.getLocalName()) && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) { array.add(n); } } } Element[] elems = new Element[array.size()]; return (Element[]) array.toArray(elems); }