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
/** * Returns the value of the first child node of the given node, and asserts * that the child node is a Text node.//from w ww. j a va2 s.c om * * @param node a node containing a single Text child node * @return the value of the child Text node */ public static String getChildText(Node node) { Node child = node.getFirstChild(); assert (child.getNodeType() == Node.TEXT_NODE); return child.getNodeValue(); }
From source file:Main.java
public static final String getCDATA(Element elem, boolean trim) { StringBuilder sb = new StringBuilder(); NodeList nl = elem.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node nc = nl.item(i);// ww w .j av a 2 s.c o m if (nc.getNodeType() == Node.CDATA_SECTION_NODE) { sb.append(((Text) nc).getData()); } else if (nc.getNodeType() == Node.TEXT_NODE) { String txt = ((Text) nc).getData(); if (trim) { txt = txt.trim(); } sb.append(txt); } } return sb.toString(); }
From source file:Main.java
public static String getTextContent(Element element) { StringBuilder buffer = new StringBuilder(); element.normalize();// w w w .jav a2 s . c om NodeList children = element.getChildNodes(); for (int i = 0, n = children.getLength(); i < n; i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE) { buffer.append(child.getNodeValue()); } } return buffer.toString().trim(); }
From source file:Main.java
/** * Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only * contain white spaces.//from ww w. j a va 2 s . co m * * @param parent * the parent Element */ public static void removeWhitespaceTextNodes(Element parent) { final NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { if (child.getNodeValue().trim().length() == 0) { parent.removeChild(child); i--; // since the child is removed counting up must be made undone } } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) { removeWhitespaceTextNodes((Element) child); } } }
From source file:Main.java
public static String getNodeValue(Node node) { if (node == null) { return null; }/*w w w . j a v a 2 s . c om*/ NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } } return null; }
From source file:Main.java
/** * Convert a node type to a string. For debug purpose only. * /*from w w w .j av a 2s . co m*/ * @param nodeType * the node type * @return the string * @throws Exception * the exception */ public static String nodeTypeToString(short nodeType) throws Exception { if (nodeType == Node.ELEMENT_NODE) return "ELEMENT_NODE"; if (nodeType == Node.ATTRIBUTE_NODE) return "ATTRIBUTE_NODE"; if (nodeType == Node.TEXT_NODE) return "TEXT_NODE"; if (nodeType == Node.CDATA_SECTION_NODE) return "CDATA_SECTION_NODE"; if (nodeType == Node.ENTITY_REFERENCE_NODE) return "ENTITY_REFERENCE_NODE"; if (nodeType == Node.ENTITY_NODE) return "ENTITY_NODE"; if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) return "PROCESSING_INSTRUCTION_NODE"; if (nodeType == Node.COMMENT_NODE) return "COMMENT_NODE"; if (nodeType == Node.DOCUMENT_NODE) return "DOCUMENT_NODE"; if (nodeType == Node.DOCUMENT_TYPE_NODE) return "DOCUMENT_TYPE_NODE"; if (nodeType == Node.DOCUMENT_FRAGMENT_NODE) return "DOCUMENT_FRAGMENT_NODE"; if (nodeType == Node.NOTATION_NODE) return "NOTATION_NODE"; if (nodeType == Node.DOCUMENT_POSITION_DISCONNECTED) return "DOCUMENT_POSITION_DISCONNECTED"; if (nodeType == Node.DOCUMENT_POSITION_PRECEDING) return "DOCUMENT_POSITION_PRECEDING"; if (nodeType == Node.DOCUMENT_POSITION_FOLLOWING) return "DOCUMENT_POSITION_FOLLOWING"; if (nodeType == Node.DOCUMENT_POSITION_CONTAINS) return "DOCUMENT_POSITION_CONTAINS"; if (nodeType == Node.DOCUMENT_POSITION_CONTAINED_BY) return "DOCUMENT_POSITION_CONTAINED_BY"; if (nodeType == Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) return "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC"; throw new Exception("Unknown value : " + nodeType); }
From source file:Main.java
public static Node getTextChild(Element aNode) { for (Node i = aNode.getFirstChild(); i != null; i = i.getNextSibling()) if (i.getNodeType() == Node.TEXT_NODE) return i; return null;//from w ww . java 2s . co m }
From source file:Main.java
public static String getNodeText(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodes = node.getChildNodes(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) { Node myNode = childNodes.item(i); buffer.append(myNode.getNodeValue()); }/*from ww w . j a va 2 s .c o m*/ } return buffer.toString(); } return ""; }
From source file:Main.java
public static String getElementText(Element n) { NodeList childNodes = n.getChildNodes(); String result = new String(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) { result += node.getNodeValue(); }// w ww. j a v a2 s . c o m } return result; }
From source file:Main.java
public static String getTextNode(Element aNode) { for (Node i = aNode.getFirstChild(); i != null; i = i.getNextSibling()) if (i.getNodeType() == Node.TEXT_NODE) return i.getNodeValue(); return null;//from w w w . j ava 2 s.c om }