List of usage examples for org.w3c.dom NodeList getLength
public int getLength();
From source file:Main.java
public static Element findElement(Element parent, String uri, String localName) { NodeList nl = parent.getElementsByTagNameNS(uri, localName); if (nl.getLength() > 0) { return (Element) nl.item(0); } else {/*from w ww .j av a 2 s . c o m*/ return null; } }
From source file:Main.java
/** * Find node by node while ignoring the case. * // w ww . j av a 2s . c o m * @param tagName node tag (case-insensitive). * @param nodes a list of nodes to look through. * @return a node name if the node is found, or null otherwise. */ public static Node getNode(String tagName, NodeList nodes) { for (int x = 0; x < nodes.getLength(); x++) { Node node = nodes.item(x); if (node.getNodeName().equalsIgnoreCase(tagName)) { return node; } } return null; }
From source file:Main.java
/** * Get the value of the first tag found with the given name in the given document<br/>. * * @param doc//from w ww . j a va 2 s . c om * @param tagName * @return the value of the node or the empty string, if no node with this name exits in this document */ public static String getTagValue(Document doc, String tagName) { NodeList tagList = doc.getElementsByTagName(tagName); if (tagList.getLength() > 0) { return tagList.item(0).getFirstChild().getNodeValue().trim(); } return ""; }
From source file:Main.java
public static List<Element> getMultipleElementsByName(Node node, String name) { List<Element> elements = new ArrayList<Element>(); NodeList nodeList = node.getChildNodes(); int count = nodeList.getLength(); for (int i = 0; i < count; i++) { Node child = nodeList.item(i); if (child instanceof Element && child.getNodeName().equals(name)) { elements.add((Element) child); }// ww w . j a v a2 s . c om } return elements; }
From source file:Main.java
public static Element[] getChildrenByName(Element e, String name) { NodeList nl = e.getChildNodes(); int max = nl.getLength(); LinkedList<Node> list = new LinkedList<Node>(); for (int i = 0; i < max; i++) { Node n = nl.item(i);/* ww w . j ava 2s .com*/ if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) { list.add(n); } } return list.toArray(new Element[list.size()]); }
From source file:Main.java
public static Node getFirstChildByTagName(Node parent, String tagName) { NodeList nodeList = parent.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName)) return node; }// w w w.j a va 2 s. com return null; }
From source file:Main.java
public static void removeContent(Element element) { NodeList nodeList = element.getChildNodes(); while (nodeList.getLength() > 0) { element.removeChild(nodeList.item(0)); }/* w ww .ja v a 2s . c o m*/ }
From source file:Main.java
/** * @return the child element, throws exception if the parent does not * have exactly one sub element with name */// w w w. j a v a2 s. c o m public static Element getSingleSubElement(Element parentElement, String name) { NodeList nodes = parentElement.getElementsByTagName(name); if (nodes.getLength() != 1) { throw new IllegalArgumentException("Expected the element " + parentElement.getNodeName() + " to have one child called " + name + " but found " + nodes.getLength()); } return (Element) nodes.item(0); }
From source file:Main.java
public static List<Element> getChildElements(Element elt, String name) { List<Element> list = new ArrayList<Element>(); NodeList nodes = elt.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) if (name.equals(child.getNodeName())) list.add((Element) child); }/*from w w w .j a v a 2s. c o m*/ return list; }
From source file:Main.java
/** * Removes all of the child nodes from a parent node. */// w w w . j a va2 s.com public static void emptyNode(final Node parent) { final NodeList childNodes = parent.getChildNodes(); for (int i = childNodes.getLength() - 1; i >= 0; i--) { final Node childNode = childNodes.item(i); childNode.getParentNode().removeChild(childNode); } }