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> getSubChildElementList(Element ele, String[] tagnames) { if (ele == null) { return null; }//from w w w . j a va 2 s . co m List<Element> v = new ArrayList<Element>(); // boolean isall = false; NodeList tmpnl = ele.getChildNodes(); Node tmpn = null; int k; for (k = 0; k < tmpnl.getLength(); k++) { tmpn = tmpnl.item(k); if (tmpn.getNodeType() != Node.ELEMENT_NODE) { continue; } Element eee = (Element) tmpn; String noden = eee.getNodeName(); int p = noden.indexOf(':'); if (p >= 0) noden = noden.substring(p + 1); for (String tagname : tagnames) { if (tagname.equals(noden) || tagname.equals("*")) { v.add(eee); break; } } } return v; }
From source file:Main.java
public static List<Element> elements(final Element father, final String ns, final String localName) { final List<Element> matchingElements = new ArrayList<Element>(); final NodeList nl = father.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node n = nl.item(i); if (n instanceof Element && n.getLocalName() != null && n.getLocalName().equals(localName) && n.getNamespaceURI() != null && n.getNamespaceURI().equals(ns)) { matchingElements.add((Element) n); }/*from w w w. jav a2 s . co m*/ } return matchingElements; }
From source file:Main.java
/** * Get the content of the given element. * * @param element The element to get the content for. * @param defaultStr The default to return when there is no content. * @return The content of the element or the default. *//*from w w w. j av a2 s . c o m*/ public static String getElementContent(Element element, String defaultStr) throws Exception { if (element == null) { return defaultStr; } final NodeList children = element.getChildNodes(); final StringBuilder result = new StringBuilder(""); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.TEXT_NODE || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) { result.append(children.item(i).getNodeValue()); } // else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) { // // Ignore comment nodes // } } return result.toString().trim(); }
From source file:Main.java
/** * Returns the child element with the specified tagName for the specified * parent element./*w w w . j a v a 2s .c om*/ * @param parent The parent whose child we're looking for. * @param tagName the name of the child to find * @return The child with the specified name or null if no such child was * found. * @throws NullPointerException if parent or tagName are null */ public static Element findChild(Element parent, String tagName) { if (parent == null || tagName == null) throw new NullPointerException( "Parent or tagname were null! " + "parent = " + parent + "; tagName = " + tagName); 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
public static List<Element> getCurrentLevelChildNodes(Element ele, String tagName) { List<Element> elements = new ArrayList<Element>(); if (ele == null) { return elements; }//from w ww . j a v a 2s. c o m NodeList nl = ele.getChildNodes(); if (nl != null && nl.getLength() > 0) { for (Integer i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && ((Element) node).getNodeName().equals(tagName)) { elements.add((Element) node); } } } return elements; }
From source file:Main.java
/** * Get the first child element of the specified (root) element that has the specified name. If no element by the * specified name is found, null is returned. Note that more than one element with the same name may be a child of * root. This method simply returns the first one. * //from w w w.j a va 2s . com * @param root The element to search. * @param name The local name of the element to look for. * @return The element if found, null otherwise. */ public static Element getElement(Element root, String name) { if (root == null || name == null || name.length() <= 0) throw new IllegalArgumentException("Null or invalid argument passed to XmlUtil.getElement()"); NodeList lst = root.getChildNodes(); int size = lst.getLength(); name = localName(name); for (int i = 0; i < size; i++) { Node node = lst.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String nodeName = localName(node); if (name.equals(nodeName)) return (Element) node; } } return null; }
From source file:Main.java
public static List<Element> getChildNodes(Element ele, String tagName) { List<Element> elements = new ArrayList<Element>(); if (ele == null) { return elements; }//from w w w . j a v a2s .c o m NodeList nl = ele.getChildNodes(); if (nl != null && nl.getLength() > 0) { for (Integer i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && ((Element) node).getNodeName().equals(tagName)) { elements.add((Element) node); } } } return elements; }
From source file:Main.java
/** * Returns an iterator over the children of the given element with * the given tag name.// ww w . ja v a 2 s . co m * * @param element The parent element * @param tagName The name of the desired child * @return An interator of children or null if element is null. */ public static Iterator<Element> getChildrenByTagName(Element element, String tagName) { if (element == null) return null; // getElementsByTagName gives the corresponding elements in the whole // descendance. We want only children NodeList children = element.getChildNodes(); ArrayList<Element> goodChildren = new ArrayList<Element>(); for (int i = 0; i < children.getLength(); i++) { Node currentChild = children.item(i); if (currentChild.getNodeType() == Node.ELEMENT_NODE && ((Element) currentChild).getTagName().equals(tagName)) { goodChildren.add((Element) currentChild); } } return goodChildren.iterator(); }
From source file:Main.java
/** * Checks if is element exists by tag name. * @param element the element/*from w ww .j ava2 s . co m*/ * @param tagName the tag name * @return true, if is element exists by tag name */ public static boolean isElementExistsByTagName(final Element element, final String tagName) { if (element == null || tagName == null || tagName.isEmpty()) { return false; } // Extract all children on element final NodeList res = element.getChildNodes(); for (int i = 0; i < res.getLength(); i++) { final Node node = res.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { final Element elem = (Element) node; // Check matching with tagname expected if (elem.getTagName().equals(tagName)) { return true; } } } return false; }
From source file:Main.java
public static Element element(final Element element, final String tagName) { Element childElement = null;//from ww w . ja va 2 s.c o m if (element != null) { final NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength() && childElement == null; i++) { final Node child = nodeList.item(i); if (Element.class.isAssignableFrom(child.getClass()) && getTagLocalName((Element) child).equals(tagName)) { childElement = (Element) child; } } } return childElement; }