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 List<Element> getElements(final Element element, final String tagName) { final ArrayList<Element> elements = new ArrayList<>(); final NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final String nodeName = node.getNodeName(); if ((nodeName != null) && nodeName.equals(tagName)) { elements.add((Element) node); }/*from ww w . ja v a 2 s. c o m*/ } } return elements; }
From source file:Main.java
/** * Get all immediate element children with the given name. This differs from {@link * org.w3c.dom.Element#getElementsByTagName getElementsByTagName} in that this only returns * direct children, not all descendents. * * @param parent the parent node/*from w ww . j a va 2s . c o m*/ * @param childName the name of the children elements to get; may be null or "*" to indicate all * element children * @return the list of children; may be empty */ public static NodeList getChildren(Element parent, String childName) { final NodeList children = parent.getChildNodes(); final String filter = (childName != null && !childName.equals("*")) ? childName : null; return new NodeList() { private List elems; { elems = new ArrayList(); for (int idx = 0; idx < children.getLength(); idx++) { Node n = children.item(idx); if (n.getNodeType() == Node.ELEMENT_NODE && (filter == null || n.getNodeName().equals(filter))) elems.add(n); } } public Node item(int index) { return (Node) elems.get(index); } public int getLength() { return elems.size(); } }; }
From source file:Main.java
public static String getTextContent(Element element) { StringBuilder buffer = new StringBuilder(); element.normalize();// w w w . j av a 2 s .co m NodeList children = element.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE) { buffer.append(child.getNodeValue()); } } return buffer.toString().trim(); }
From source file:Main.java
public static Element GetChildElement(Element element, String name) { if (element != null && name != null && !name.isEmpty()) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && name.equals(node.getNodeName())) { return (Element) node; }//from ww w . j a v a2 s .c o m } } return null; }
From source file:Main.java
public static List<Element> getSubElement(Element element) { ArrayList<Element> result = new ArrayList<Element>(); NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { switch (childNodes.item(i).getNodeType()) { case Node.ELEMENT_NODE: result.add((Element) childNodes.item(i)); break; }//from w w w . j a v a2 s.c o m } return result; }
From source file:Main.java
public static String getSimpleElementText(Element node) { if (node == null) return null; StringBuffer sb = new StringBuffer(); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Text) { sb.append(child.getNodeValue()); }// w w w . j a v a2 s .c o m } return sb.toString(); }
From source file:Main.java
public static ArrayList<Element> getChildElements(Element parent, String childName) { ArrayList<Element> childElements = new ArrayList<Element>(); NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (childName == null || childNode.getNodeName().equals(childName)) { childElements.add((Element) childNode); }/*from w w w .j ava2s. co m*/ } } return childElements; }
From source file:Main.java
/** Helper method - Determines if a <I>Node</I> has any non-<I>CharacterData</I> nodes. This call is stricter than the <CODE>Node.hasChildNodes</CODE> call. */// w w w . j a v a 2 s.co m public static boolean hasNonCharacterChildren(Element pElement) { // Do the quick test. if (!pElement.hasChildNodes()) return false; NodeList pNodeList = pElement.getChildNodes(); int nLength = pNodeList.getLength(); // Find a non-character data node. for (int i = 0; i < nLength; i++) if (!(pNodeList.item(i) instanceof CharacterData)) return true; return false; }
From source file:Main.java
/** * Return the index'th child element of parent that has the given element * tag name.// w ww . j a v a 2 s .co m * * @param index * Index of the child to return * @param name * Element tag name to search for * @param parent * Parent to search * @return index'th child element of parent that has the given name */ public static Element getChildElementByTagName(int index, String name, Element parent) { if (parent == null) { return null; } NodeList children = parent.getChildNodes(); if (children == null) { return null; } int count = 0; for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node instanceof Element) { Element e = (Element) node; if (e.getTagName().equals(name) && (count++ == index)) { return e; } } } return null; }
From source file:Main.java
/** * Gets the list of immediate child elements with the given tag name. * /*from w ww .j a va 2s . c om*/ * @param element * @param tagName * @return list of {@link Element} objects */ public static List<Element> getChildElementsByTagName(Element element, String tagName) { List<Element> elements = new ArrayList<Element>(); NodeList list = element.getChildNodes(); int size = list.getLength(); if (size > 0) { for (int i = 0; i < size; i++) { Node node = list.item(i); if (node instanceof Element) { Element e = (Element) node; if (e.getTagName().equals(tagName)) { elements.add(e); } } } } return elements; }