List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
/** Converts a NodeList to an Iterable of Nodes. */ public static Iterable<Node> toIterable(NodeList nodeList) { List<Node> nodes = new ArrayList<>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { nodes.add(nodeList.item(i));/*from w ww . jav a 2 s .com*/ } return nodes; }
From source file:Main.java
public static boolean containsElementByValue(NodeList elements, String value) { for (int i = 0; i < elements.getLength(); i++) { Node node = elements.item(i); if (node.getTextContent().equals(value)) { return true; }//from w w w .j a v a 2 s . co m } return false; }
From source file:Main.java
private static String getTextValue(Element ele, String tagName) { String textVal = null;/* w w w .j a v a 2 s . c o m*/ NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; }
From source file:Main.java
private final static boolean isParentNode(Node element) { NodeList elements = element.getChildNodes(); for (int i = 0; i < elements.getLength(); i++) { Node child = elements.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { return true; }/*w w w . j ava2s. c om*/ } return false; }
From source file:Main.java
/** * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree. *///from w ww . j a va 2 s.co m public static void stripEmptyTextNodes(Node n) { NodeList children = n.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { Node c = children.item(i); if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE && c.getTextContent().trim().length() == 0) { n.removeChild(c); i--; length--; children = n.getChildNodes(); } else { stripEmptyTextNodes(c); } } }
From source file:Main.java
/** Returns the first element with the given name from a document. * /*from w w w. j ava 2 s . c om*/ * @param doc Document to be searched * @param name The element name to search for * @return Node representing the element */ public static Node getNodeByName(Document doc, String name) { Node retVal = null; NodeList nl = doc.getElementsByTagName(name); if (nl != null && nl.getLength() != 0) retVal = nl.item(0); return retVal; }
From source file:TestDOM.java
public static Element getFirstElement(Element element, String name) { NodeList nl = element.getElementsByTagName(name); if (nl.getLength() < 1) throw new RuntimeException("Element: " + element + " does not contain: " + name); return (Element) nl.item(0); }
From source file:Main.java
public static Node getChildNode(Node node, String childNodeName) { final NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node childNode = children.item(i); if (childNode.getNodeName().equals(childNodeName)) { return childNode; }// w w w . ja va 2 s . c o m } return null; }
From source file:Main.java
/** * Returns a node's first child node that is an element. * @param parent/*from www .j a v a 2s. c om*/ * @return first child element, or null */ public static Element getFirstChildElement(Node parent) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }
From source file:Main.java
/** * Return the contained text within an Element. Returns null if no text found. *//*from w w w . j a va 2s . com*/ public final static String getElementText(Element element) { NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node c = nl.item(i); if (c instanceof Text) { return ((Text) c).getData(); } } return null; }