List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:Main.java
public static String getText(Node node) { StringBuffer result = new StringBuffer(); if (!node.hasChildNodes()) return ""; NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) {/*from w w w .j a v a2 s .co m*/ 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)); } } return result.toString(); }
From source file:Main.java
public static Node[] findNodesByTagName(Node parentNode, String tagName) { ArrayList<Node> nodes = new ArrayList<Node>(); for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) { Node node = parentNode.getChildNodes().item(i); if (node.getNodeName().equals(tagName)) nodes.add(node);/*from w w w .j av a2 s .com*/ if (node.hasChildNodes()) { Node[] foundChildNodes = findNodesByTagName(node, tagName); for (int j = 0; j < foundChildNodes.length; j++) nodes.add(foundChildNodes[j]); } } Node[] returnNodes = new Node[nodes.size()]; return nodes.toArray(returnNodes); }
From source file:Main.java
public static Map<String, Object> convertNodeToMap(Node node) { NodeList nodeList = node.getChildNodes(); Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node nodec = nodeList.item(i); String key = nodec.getNodeName(); Object value = null;// w w w. j a v a 2 s .c o m if (nodec.hasChildNodes()) { NodeList nodeListc = nodec.getChildNodes(); if (nodeListc.getLength() == 1) { Node noded = nodeListc.item(0); short type = noded.getNodeType(); if (type == 3 || type == 4) { value = noded.getNodeValue(); } if (noded.getNodeType() == 1) { value = convertNodeToMap(nodec); } } else { value = convertNodeToMap(nodec); } } map.put(key, value); } return map; }
From source file:Main.java
private static String getValueByTagName(Node n, String tag) { if (n == null) return null; if (tag.equals(n.getLocalName())) return n.getFirstChild().getNodeValue(); if (n.hasChildNodes()) return getValueByTagName(n.getFirstChild(), tag); else if (n.getNextSibling() != null) return getValueByTagName(n.getNextSibling(), tag); else/* w w w .ja va 2 s. c o m*/ return getValueByTagName(n.getParentNode().getNextSibling(), tag); }
From source file:com.ewcms.common.io.HtmlStringUtil.java
public static String getPureText(Node node) { if (!node.hasChildNodes() && isTextNode(node)) return node.getNodeValue(); if (isFiltered(node)) return ""; if (node.hasAttributes()) { Node a = node.getAttributes().getNamedItem("style"); if (a != null) { String style = a.getNodeValue(); Pattern p = Pattern.compile("display\\s*\\:\\s*none", 2); if (p.matcher(style).find()) return ""; }/*from w w w . j a v a2s. c o m*/ } StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node child = list.item(i); String name = child.getNodeName(); sb.append(getPureText(child)); sb.append(" "); if (name.equals("TR") || name.equals("P") || name.equals("DIV")) sb.append("\n"); } return sb.toString(); }
From source file:DOMUtil.java
/** * Remove all text nodes below this node * * @param node The starting node for the search. */// www . j a va 2s.c om public static void removeAllTextNodes(Node node) { if (node == null) return; if (!node.hasChildNodes()) return; NodeList nl = node.getChildNodes(); for (int i = nl.getLength() - 1; i >= 0; i--) { Node n = (Node) nl.item(i); if (n instanceof Text) node.removeChild(n); else removeAllTextNodes(n); } }
From source file:com.hueemulator.lighting.utils.TestUtils.java
private static Set<Object> getChildSet(Node node, String basePath) { Set<Object> childSet = new HashSet<Object>(); if (!node.hasChildNodes() && !node.getTextContent().trim().equals("")) { childSet.add(basePath + ":" + node.getTextContent()); } else {/*from w w w . j av a 2 s . com*/ NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { childSet.add(getChildSet(children.item(i), basePath + "/" + node.getNodeName())); } } return childSet; }
From source file:Main.java
static void dumpXpath(Node node, PrintStream printer) { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node item = list.item(i); printer.println("Xpath: " + getXPath(item)); printer.println("Text Content: " + item.getTextContent()); if (item.hasChildNodes()) { dumpXpath(item, printer);// w w w.j a v a2s .c om } } }
From source file:Main.java
public static Node findNode(Node root, String elementName, boolean deep, boolean elementsOnly) { //Check to see if root has any children if not return null if (!(root.hasChildNodes())) return null; //Root has children, so continue searching for them Node matchingNode = null;//from www .j a va2 s .c o m String nodeName = null; Node child = null; NodeList childNodes = root.getChildNodes(); int noChildren = childNodes.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childNodes.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; if (deep) matchingNode = findNode(child, elementName, deep, elementsOnly); } else break; } if (!elementsOnly) { NamedNodeMap childAttrs = root.getAttributes(); noChildren = childAttrs.getLength(); for (int i = 0; i < noChildren; i++) { if (matchingNode == null) { child = childAttrs.item(i); nodeName = child.getNodeName(); if ((nodeName != null) & (nodeName.equals(elementName))) return child; } else break; } } return matchingNode; }
From source file:Main.java
public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars) throws Exception { if (qvars == null) { throw new Exception("qvars array must not be null"); }// ww w .java 2s . c o m if (nQ.hasChildNodes()) { int nQChildLength = nQ.getChildNodes().getLength(); if (nN.hasChildNodes() && (!considerLength || nQChildLength == nN.getChildNodes().getLength())) { //loop through all childnodes for (int i = 0; i < nQChildLength; i++) { //System.out.println("recurse to "+ nQ.getChildNodes().item( i )+"vs"+nN.getChildNodes().item( i )); //DEBUG if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength, qvars)) { return false; } } } } if (nQ.getNodeName().equals("mws:qvar")) { String qvarName = nQ.getAttributes().getNamedItem("name").getNodeValue(); if (qvars.containsKey(qvarName)) { return compareNode(qvars.get(qvarName), nN, considerLength, qvars); } else { qvars.put(qvarName, nN); return true; } } else { if (nQ.getNodeName().equals(nN.getNodeName())) { try { return nQ.getNodeValue().trim().equals(nN.getNodeValue().trim()); } catch (NullPointerException e) { //NodeValue does not exist return true; } } else { return false; } } }