List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
public static Element nthElement(Element parent, String elementName, int index) { NodeList nds = parent.getElementsByTagName(elementName); if (nds.getLength() < index) throw new NoSuchElementException(); Element node = (Element) nds.item(index - 1); return node;/*from w w w . j a va2s. co m*/ }
From source file:Main.java
public static String getChildText(Element parent, String childName) { NodeList list = parent.getElementsByTagName(childName); if (list.getLength() > 1) { throw new IllegalStateException("Multiple child elements with name " + childName); } else if (list.getLength() == 0) { return null; }//from w ww. j av a 2s .c o m Element child = (Element) list.item(0); return getText(child); }
From source file:Main.java
public static Element getChildWithAttribute(Element parent, String attrName) { 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 (!"".equals(element.getAttribute(attrName))) { return element; }//from ww w . j a va2s. c o m } } return null; }
From source file:Main.java
static private Node getNode(Document doc, String nodeName, String textValue) { NodeList nodelist = doc.getElementsByTagName(nodeName); for (int i = 0; i < nodelist.getLength(); i++) { if (nodelist.item(i).getTextContent().equals(textValue)) { return nodelist.item(i); }/*from w w w .j a va 2 s. com*/ } return null; }
From source file:Main.java
public static Element findChildElement(Element parent, String name) { Element result = null;/*w w w . j ava2 s . c om*/ NodeList nodes = parent.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeName().equals(name)) { result = (Element) node; break; } } return result; }
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; }// w w w . j a v a 2s. c o m } } return null; }
From source file:Main.java
public static void removeAllAttributes(Node node, String attrName) { // check if this node contains the attribute and remove it NamedNodeMap attrs = node.getAttributes(); if (attrs != null && attrs.getNamedItem(attrName) != null) { attrs.removeNamedItem(attrName); }/*from w w w. j av a 2s . c om*/ // process recursively all children NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { // Get child node Node childNode = list.item(i); // Visit child node removeAllAttributes(childNode, attrName); } }
From source file:Main.java
/** * Returns the first child element found with the specified tag name * @param node The node to search/*www . ja va 2 s.com*/ * @param element The element tag name to search for * @return The matching element Node */ public synchronized static Node getFirstElementWithTagName(Node node, String element) { if (node != null && element != null) { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element)) return n; } } return null; }
From source file:Main.java
public static Element get_child_with_attr(Node parent, String child_name, String attr_name, String attr_value) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node child = children.item(i); if (child.getNodeName().equals(child_name)) { if (child instanceof Element) { Element e = (Element) child; if (e.getAttribute(attr_name).equals(attr_value)) return e; }//from ww w . ja v a 2 s . c o m } } return null; }
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. *///from w ww . j av a 2 s . com 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; }