List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:Main.java
public static Element findElement(Node sourceElm, String attrName, String sAttrValue) { if (sourceElm.hasChildNodes()) { NodeList nodes = sourceElm.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { if (Node.ELEMENT_NODE == nodes.item(i).getNodeType()) { Element elm = findElement(nodes.item(i), attrName, sAttrValue); if (((Element) elm).getAttribute(attrName).equals(sAttrValue)) { return (Element) elm; }//from www . j av a 2 s.c o m } } } else { if (sourceElm.hasAttributes() && ((Element) sourceElm).getAttribute(attrName).equals(sAttrValue)) { return (Element) sourceElm; } } return (Element) sourceElm; }
From source file:Main.java
public static void setByPath(Node doc, String path, String value) { Node node = getNodeByPath(doc, path); if (node.hasChildNodes() && node.getFirstChild().getNodeType() == Node.TEXT_NODE) { node.getFirstChild().setTextContent(value); } else {/* ww w. ja v a 2 s. co m*/ node.setNodeValue(value); } }
From source file:Main.java
/** * Get the Value of a node that has an embedded text node as a child * @param doc the Document to search for the node name * @param nodeName the name of the node to get the value * @return the value of the text node child *///ww w .j a v a 2 s . c o m public static String getNodeTextValue(Document doc, String nodeName) { String value = null; NodeList nodes = doc.getElementsByTagName(nodeName); if (nodes.getLength() > 0) { Node node = nodes.item(0); if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } } return replaceSpecialCharacters(value); }
From source file:Main.java
/** * Get the value of a node assuming there is one child node with a text value * // w w w.j a v a 2 s. c om * @param node * @return the value */ public static String getNodeTextValue(Node node) { String value = null; if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } return replaceSpecialCharacters(value); }
From source file:Main.java
private static void resolveLeafNodeValue(Node node) { if (node != null) { Element element = (Element) node; NodeList childNodeList = element.getChildNodes(); for (int j = 0; j < childNodeList.getLength(); j++) { Node chileNode = childNodeList.item(j); if (!chileNode.hasChildNodes()) { String nodeValue = resolveSystemProperty(chileNode.getTextContent()); childNodeList.item(j).setTextContent(nodeValue); } else { resolveLeafNodeValue(chileNode); }/*from w ww.j av a2 s. c o m*/ } } }
From source file:Main.java
/** * Finds the node of the argument name in the tree under the argument node. * @param name The name of the node to search for, case insensitive. * @param node The root node of the tree under which to look for the named node. * @return the first found occurrence of the named node (breadth first), * or null if the node is not found.//from www. j a va 2 s. c om */ public static Node findChild(String name, Node node) { if (node == null) return null; if (node.hasChildNodes()) { NodeList kids = node.getChildNodes(); int numKids = kids.getLength(); for (int index = 0; index < numKids; ++index) { Node kid = kids.item(index); if (kid.getNodeName().equalsIgnoreCase(name)) return kid; } // for for (int index = 0; index < numKids; ++index) { Node found = findChild(name, kids.item(index)); if (found != null) return found; } // for } // if return null; }
From source file:Main.java
public static String getText(final Node node) { final StringBuilder result = new StringBuilder(); if (!node.hasChildNodes()) return ""; final NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node subnode = list.item(i); if (subnode.getNodeType() == Node.TEXT_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { // Recurse into the subtree for text // (and ignore comments) result.append(getText(subnode)); }/*from w ww.jav a 2 s .co m*/ } return result.toString(); }
From source file:Main.java
public static String getText(Node elem) { if (elem.getNodeType() == Element.TEXT_NODE) return elem.getTextContent(); else if (elem.hasChildNodes()) return elem.getFirstChild().getTextContent(); return ""; }
From source file:Main.java
public static Node findChildWithTagName(Node parent, String tagName) { if (parent == null) { return null; }/*from ww w .j a v a 2s . c om*/ NodeList childs = parent.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); if (child.getNodeName().equals(tagName)) { return child; } else if (child.hasChildNodes()) { Node result = findChildWithTagName(child, tagName); if (result != null) { return result; } } } return null; }
From source file:Main.java
public static void getElementsByTagName(Node parent, String tagName, List<Node> result) { if (parent == null) { return;/*from ww w.j a v a 2 s . c o m*/ } NodeList childs = parent.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { Node child = childs.item(i); if (child.getNodeName().equals(tagName)) { result.add(child); } else if (child.hasChildNodes()) { getElementsByTagName(child, tagName, result); } } }