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 Element getChild(Element parent, String name) { NodeList children = parent.getChildNodes(); final int length = children.getLength(); for (int index = 0; index < length; index++) { Node node = children.item(index); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; }//from www .j av a 2 s . c om Element element = (Element) node; if (name.equalsIgnoreCase(element.getNodeName())) { return element; } } return null; }
From source file:Main.java
public static Element[] getChildrenByName(Element parentElement, String childrenName) { NodeList nl = parentElement.getChildNodes(); int max = nl.getLength(); LinkedList<Node> list = new LinkedList<Node>(); for (int i = 0; i < max; i++) { Node n = nl.item(i);/* w ww .j a va 2 s .c om*/ if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) { list.add(n); } } return list.toArray(new Element[list.size()]); }
From source file:Main.java
public static Element getChildElement(Element parent, String childName, String childNamespace) { NodeList ns = parent.getChildNodes(); for (int i = 0; i < ns.getLength(); i++) { Node n = ns.item(i);// w w w . j a va2s .c o m if (n instanceof Element) { Element child = (Element) n; if (childName.equals(child.getLocalName()) && childNamespace.equals(child.getNamespaceURI())) { return child; } } } return null; }
From source file:Main.java
/** * Returns the first child element for the given name *//* ww w .ja v a2 s .c o m*/ public static Element firstChild(Element element, String name) { NodeList nodes = element.getChildNodes(); if (nodes != null) { for (int i = 0, size = nodes.getLength(); i < size; i++) { Node item = nodes.item(i); if (item instanceof Element) { Element childElement = (Element) item; if (name.equals(childElement.getTagName())) { return childElement; } } } } return null; }
From source file:Main.java
public static Element getElementById(Element element, String id) { return getElementById(element.getChildNodes(), id); }
From source file:Main.java
public static Element getFirstSubElement(Element element) { NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { switch (childNodes.item(i).getNodeType()) { case Node.ELEMENT_NODE: return (Element) childNodes.item(i); }/* ww w .jav a 2 s. c om*/ } return null; }
From source file:Main.java
/** * @return a List of child Elements/*from www.j a v a2s. c o m*/ */ public static List<Element> getChildren(Element element) throws Exception { NodeList nodes = element.getChildNodes(); ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { ret.add((Element) childNode); } } return ret; }
From source file:Main.java
public static void stripWhitespace(Element element) { final NodeList childNodeList = element.getChildNodes(); for (int i = 0; i < childNodeList.getLength(); i++) { Node node = childNodeList.item(i); switch (node.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: final String text = ((CharacterData) node).getData(); if (WhitespacePattern.matcher(text).matches()) { element.removeChild(node); --i;//from www .j ava 2s . c om } break; case Node.ELEMENT_NODE: stripWhitespace((Element) node); break; } } }
From source file:Main.java
public static String getTextChild(Element element) { NodeList children = element.getChildNodes(); String value = null;/*from w ww . j ava 2 s.co m*/ for (int i = 0; i < children.getLength(); ++i) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE) { if (null != value) { throw new IllegalStateException( "Element " + elementToString(element) + " has more than one text child."); } else { value = child.getNodeValue(); } } } return value; }
From source file:Main.java
public static Element getChildWithAttributeValue(Element parent, String attrName, String attrValue) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node instanceof Element) { Element element = (Element) node; if (attrValue.equals(element.getAttribute(attrName))) { return element; }/* ww w .ja va2s . c o m*/ } } return null; }