List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:Main.java
/** * Search a child node by name // w w w .j a v a 2s . c o m * * @param parent the parent node * @param nodeName the node name for searching * @return Node with the specified name * @see Node * @throws Exception */ public static Node findChildNodeByName(Node parent, String nodeName) throws Exception { Node child = null; Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeName().equals(nodeName)) { child = node; break; } node = node.getNextSibling(); } return child; }
From source file:Main.java
/** * Gets the date between an opening and closing xml tag. E.g. for * <tag>xyz</tag> the string xyz would be returned. If there are multiple * nodes with the specified tag name, the data of the first node found will * be returned./*from w w w. ja va2 s.c o m*/ * * @param doc * a document. * @param tagname * the name of the tag. * @return the data of the tag or <code>null</code> if no node with the * specified tag name could be found. */ public static String getTagData(Document doc, String tagname) { Node node = getNode(doc, tagname); if (node != null) { Node child = node.getFirstChild(); if (child != null) { return child.getNodeValue(); } } return null; }
From source file:Main.java
public static String getNodeValue(final Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { Node n = node.getFirstChild(); if (n != null) { do {/* w w w .j av a 2 s . com*/ if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } while ((n = n.getNextSibling()) != null); } } return node.getNodeValue(); }
From source file:Main.java
/** * Removes text nodes that only contains whitespace. The conditions for * removing text nodes, besides only containing whitespace, are: If the * parent node has at least one child of any of the following types, all * whitespace-only text-node children will be removed: - ELEMENT child - * CDATA child - COMMENT child/*from www . ja va2 s. c o m*/ * * The purpose of this is to make the format() method (that use a * Transformer for formatting) more consistent regarding indenting and line * breaks. */ public static void cleanEmptyTextNodes(Node parentNode) { boolean removeEmptyTextNodes = false; Node childNode = parentNode.getFirstChild(); while (childNode != null) { removeEmptyTextNodes |= checkNodeTypes(childNode); childNode = childNode.getNextSibling(); } if (removeEmptyTextNodes) { removeEmptyTextNodes(parentNode); } }
From source file:Main.java
/** * Returns the text inside the passed XML node. * @param node The node to extract from/*from w w w . ja v a 2 s .c o m*/ * @param defaultValue The default value if no text is found * @return the extracted text */ public static String getNodeTextValue(final Node node, final String defaultValue) { try { String s = node.getFirstChild().getNodeValue(); return s == null ? defaultValue : s; } catch (Exception ex) { return defaultValue; } }
From source file:Main.java
/** * Returns the value of given node/* ww w .j av a 2 s .c o m*/ * @param node node which value is to be returned * @return the value of given node */ public static String getNodeValue(Node node) { String retval = ""; try { Node firstChild = node.getFirstChild(); if (firstChild != null) retval = firstChild.getNodeValue(); } catch (Exception e) { return ""; } return retval; }
From source file:Main.java
public static Vector<Element> getChildElemsByName(final String name, final Node parent) { Vector<Element> v = new Vector<Element>(); Element elem = null;/*from w w w . j a v a2 s. com*/ for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) { if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (childNode.getNodeName() == name) { elem = (Element) childNode; v.add(elem); } } } return v; }
From source file:Main.java
/** * get the node following the node in parameter * // ww w.j a va 2 s . c o m * @param n * the n * @return the node */ public static Node next(Node n) { if (n == null) { return null; } Node currentNode = n; if (currentNode.getChildNodes().getLength() > 0) { currentNode = currentNode.getFirstChild(); } else { if (currentNode.getNextSibling() != null) { currentNode = currentNode.getNextSibling(); } else { Node oldCurrentNode = currentNode; currentNode = currentNode.getParentNode().getNextSibling(); while (oldCurrentNode != null && currentNode == null) { oldCurrentNode = oldCurrentNode.getParentNode(); if (oldCurrentNode != null) { currentNode = oldCurrentNode.getNextSibling(); } } } } return currentNode; }
From source file:Main.java
/** * Evaluates the XPath expression in the specified context and returns the found items as a List. * * @param node the XML document to evaluate * @param expression the compiled XPath expression * @return the list of elements found//from w ww. ja va 2 s . co m */ public static List<String> getListValue(Node node, XPathExpression expression) { try { NodeList nodeList = (NodeList) expression.evaluate(node, XPathConstants.NODESET); List<String> list = new ArrayList<String>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node item = nodeList.item(i); list.add(item.getFirstChild().getNodeValue()); } return list; } catch (XPathExpressionException e) { // Try to evaluate in string context: String value = getStringValue(node, expression); if (value != null) { List<String> list = new ArrayList<String>(1); list.add(value); return list; } return Collections.emptyList(); } }
From source file:Main.java
public static synchronized Node[] getChildNodes(Node node) { if (node == null) { return null; }//from ww w .j a va 2s .c om Vector childs = new Vector(); for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) { childs.add((Element) n); } Node[] childNodes = new Element[childs.size()]; childs.toArray(childNodes); return childNodes; }