List of usage examples for org.w3c.dom Node TEXT_NODE
short TEXT_NODE
To view the source code for org.w3c.dom Node TEXT_NODE.
Click Source Link
Text
node. From source file:Main.java
public static String getElementText(Element elem) { StringBuilder buf = new StringBuilder(); NodeList nodeList = elem.getChildNodes(); for (int i = 0, len = nodeList.getLength(); i < len; i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.TEXT_NODE) { buf.append(n.getNodeValue()); } else {/*from w ww. j ava 2 s . c o m*/ //TODO see jsf-samples //throw new FacesException("Unexpected node type " + n.getNodeType()); } } return buf.toString(); }
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 {//ww w . j a v a 2s . c o m if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } while ((n = n.getNextSibling()) != null); } } return node.getNodeValue(); }
From source file:Main.java
private static String toString(Node node, int level, boolean indent) { StringBuilder nodeBuilder = new StringBuilder(); switch (node.getNodeType()) { case Node.TEXT_NODE: if (indent) { nodeBuilder.append(setIndent(level)); }//w w w .j a v a 2 s .com nodeBuilder.append(node.getNodeValue()); if (indent) { nodeBuilder.append("\n"); } break; case Node.ELEMENT_NODE: NamedNodeMap attributeMap; NodeList childList; if (indent) { nodeBuilder.append(setIndent(level)); } nodeBuilder.append("<"); nodeBuilder.append(((Element) node).getTagName()); attributeMap = node.getAttributes(); for (int loop = 0; loop < attributeMap.getLength(); loop++) { nodeBuilder.append(" "); nodeBuilder.append(attributeMap.item(loop).getNodeName()); nodeBuilder.append("=\""); nodeBuilder.append(attributeMap.item(loop).getNodeValue()); nodeBuilder.append("\""); } if (node.hasChildNodes()) { nodeBuilder.append(">"); if (indent) { nodeBuilder.append("\n"); } childList = node.getChildNodes(); for (int loop = 0; loop < childList.getLength(); loop++) { nodeBuilder.append(toString(childList.item(loop), level + 1, indent)); } if (indent) { nodeBuilder.append(setIndent(level)); } nodeBuilder.append("</"); nodeBuilder.append(((Element) node).getTagName()); nodeBuilder.append(">"); if (indent) { nodeBuilder.append("\n"); } } else { nodeBuilder.append("/>"); if (indent) { nodeBuilder.append("\n"); } } break; default: nodeBuilder.append("<Unkown Node Type ("); nodeBuilder.append(node.getNodeType()); nodeBuilder.append(")/>"); if (indent) { nodeBuilder.append("\n"); } } return nodeBuilder.toString(); }
From source file:Main.java
/** * Gets the value of an element. This method returns a concatenated String * from all its TEXT children./*from www . ja v a 2 s .c o m*/ * * @param element * a DOM tree element. * @return A String that contained in its TEXT children; or null if an error * occurred. */ public static String getElementValue(Element element) { if (element == null) { return null; } StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } } return sb.toString().trim(); }
From source file:Main.java
/** * Checks if a node has contents.// w ww .j a va 2 s .c om * A node is considered as <i>having content</i> when: * <ul> * <li>When the node or any of its childs contains a non-empty attribute.</li> * <li>When the node or any of its childs contains non-empty text nodes. If the * parameter <b>ignoreBlankSpaces</b> is <i>true</i>, text nodes containing * only control characters (ascii codes equal or smaller than 32) are considered * empty.</li> * </ul> * Comment and processing instruction nodes are not considered as content. * @param x The node to check. If it is null, the method returns <i>false</i>. * @param ignoreBlankSpaces If <i>true</i> text nodes containing * only control characters (ascii codes equal or smaller than 32) are considered * empty. * @return <i>true</i> if the specified node has contents. */ static public boolean NodeHasContent(Node x, boolean ignoreBlankSpaces) { int n, nn; // To parse child nodes of "x". Node child; // One of the childs of "x". int childType; // Type of the child node. String childContent; // When the child is a text node, its content. //.............................. Looks for "non-empty" nodes ................... if (x != null) { nn = x.getChildNodes().getLength(); for (n = 0; n < nn; n++) { child = x.getChildNodes().item(n); childType = child.getNodeType(); switch (childType) { // Nodes containing text: case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: if (ignoreBlankSpaces) childContent = child.getNodeValue().trim(); else childContent = child.getNodeValue(); if (childContent.length() > 0) return true; // Elements (nodes containing nodes): case Node.ELEMENT_NODE: if (NodeHasContent(child, ignoreBlankSpaces)) return true; // Any other type of node are not considered as content: default: continue; } } } //............................. If no "non-empty" node has been found .......... return false; }
From source file:Main.java
public String getNodeType(Node node) { String type;//from w w w . jav a 2 s.c o m switch (node.getNodeType()) { case Node.ELEMENT_NODE: { type = "Element"; break; } case Node.ATTRIBUTE_NODE: { type = "Attribute"; break; } case Node.TEXT_NODE: { type = "Text"; break; } case Node.CDATA_SECTION_NODE: { type = "CData section"; break; } case Node.ENTITY_REFERENCE_NODE: { type = "Entity reference"; break; } case Node.ENTITY_NODE: { type = "Entity"; break; } case Node.PROCESSING_INSTRUCTION_NODE: { type = "Processing instruction"; break; } case Node.COMMENT_NODE: { type = "Comment"; break; } case Node.DOCUMENT_NODE: { type = "Document"; break; } case Node.DOCUMENT_TYPE_NODE: { type = "Document type"; break; } case Node.DOCUMENT_FRAGMENT_NODE: { type = "Document fragment"; break; } case Node.NOTATION_NODE: { type = "Notation"; break; } default: { type = "???"; break; } } return type; }
From source file:Main.java
private static void removeEmptyTextNodes(Node parentNode) { Node childNode = parentNode.getFirstChild(); while (childNode != null) { Node nextChild = childNode.getNextSibling(); short nodeType = childNode.getNodeType(); if (nodeType == Node.TEXT_NODE) { boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty(); if (containsOnlyWhitespace) { parentNode.removeChild(childNode); }/*from w w w . j a v a 2 s . c om*/ } childNode = nextChild; } }
From source file:Main.java
static StringBuffer textContent(Node node, StringBuffer s) { if (node == null) return s; for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) { if (c.getNodeType() == Node.CDATA_SECTION_NODE) { s.append(((CDATASection) c).getNodeValue()); } else if (c.getNodeType() == Node.TEXT_NODE) { s.append(((Text) c).getNodeValue()); } else {/*from w ww . ja v a2s .co m*/ textContent(c, s); } } return s; }
From source file:Main.java
/** * Gets the value of an element. This method returns a concatenated String * from all its TEXT children./*from w ww . j av a2s . c o m*/ * * @param element * a DOM tree element. * @return A String that contained in its TEXT children; or null if an error * occurred or the input contain non Node.TEXT_NODE node. */ public static String getElementString(Element element) { if (element == null) { return null; } StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; for (int i = 0, length = nl.getLength(); i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } else { return null; } } return sb.toString().trim(); }
From source file:Main.java
public static String getElementText(Element elem) { StringBuffer buf = new StringBuffer(); NodeList nodeList = elem.getChildNodes(); for (int i = 0, len = nodeList.getLength(); i < len; i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.TEXT_NODE) { buf.append(n.getNodeValue()); } else {/*w ww . j av a 2 s .c om*/ //TODO see jsf-samples //throw new FacesException("Unexpected node type " + n.getNodeType()); } } return buf.toString(); }