List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
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 www. j av a2 s.c o 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
/** * based on public Java5 javadoc of org.w3c.dom.Node.setTextContent method *//*w w w . j a va 2 s . co m*/ public static void setTextContent(Node node, final String text) { while (node.hasChildNodes()) { node.removeChild(node.getFirstChild()); } if (text != null && text.length() > 0) { Node textNode = node.getOwnerDocument().createTextNode(text); node.appendChild(textNode); } }
From source file:Main.java
/** * Method to remove all children of g:options - element * /*from w w w. jav a2 s . c o m*/ * @param doc document with children g:options * @return document without children g:options */ public static Document removeAllOptions(Document doc) { NodeList nodes = doc.getElementsByTagName("g:options"); Node node = nodes.item(0); while (node.hasChildNodes()) node.removeChild(node.getFirstChild()); return doc; }
From source file:NodeUtils.java
/** * Returns a first child DOM Node of type ELEMENT_NODE * for the specified Node./* w w w.ja v a 2s . c om*/ */ public static Node getChildElementNode(Node xmlNode) { if (xmlNode == null || !xmlNode.hasChildNodes()) { return null; } xmlNode = xmlNode.getFirstChild(); while (xmlNode != null && xmlNode.getNodeType() != Node.ELEMENT_NODE) { xmlNode = xmlNode.getNextSibling(); } return xmlNode; }
From source file:Main.java
/** * Delete children elements for Node// w w w . j av a 2 s. c om */ public static void removeAllChildren(Node node) { if (node != null) { while (node.hasChildNodes()) { node.removeChild(node.getFirstChild()); } } }
From source file:Main.java
public static String getNormalizedText(Node node) { String res = ""; if (node.hasChildNodes()) { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { res += nl.item(i).getNodeValue(); } else { // ignore <SCRIPT> nodes ... if (!nl.item(i).getNodeName().equalsIgnoreCase("script")) res += getNormalizedText(nl.item(i)); }//from w w w . j a v a2 s. co m } } return res; }
From source file:Main.java
public static String getDeepNodeValue(Node node) { StringJoiner out = new StringJoiner(" "); if (node.hasChildNodes()) { for (int i = 0; i < node.getChildNodes().getLength(); i++) { String val = getDeepNodeValue(node.getChildNodes().item(i)); if (val != null && val.trim().length() != 0) out.add(val.trim()); }/*from w w w. j a v a 2 s .c om*/ } else if (node.getNodeValue() != null && node.getNodeValue().trim().length() != 0) { out.add(node.getNodeValue().trim()); } return out.toString(); }
From source file:Main.java
/** * Return a Node object with the name is nodeName * @param parent parent Node which contains the node nodeName * @param nodeName name of the node//w ww.ja v a2 s.co m * @return a Node object with the name is nodeName<br> * if the node parent doesn't have child nodes, null is return. */ public static Node getNode(Node parent, String nodeName) { if (parent.hasChildNodes()) { for (int i = 0; i < parent.getChildNodes().getLength(); i++) { if (parent.getChildNodes().item(i).getNodeName().equalsIgnoreCase(nodeName)) { return parent.getChildNodes().item(i); } } } return null; }
From source file:Main.java
public static String getId(Node node) { try {// w w w . j a v a 2 s . c o m NamedNodeMap nnm = node.getAttributes(); Node attrib = nnm.getNamedItem("Id"); Object ID; if (attrib.hasChildNodes()) { ID = attrib.getChildNodes().item(0).getNodeValue(); } else { ID = attrib.getNodeValue(); } return ID.toString(); } catch (Exception ex) { return ""; } }
From source file:Main.java
private static void appendChildren(Node node, StringBuffer sb) { if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { sb.append(convertDOMToString(children.item(i))); }/*from w ww .java 2 s .c o m*/ } }