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
private static String getText(Node node) { if (node == null) return null; NodeList lst = node.getChildNodes(); int size = lst.getLength(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < size; i++) { Node n = lst.item(i);//from ww w . j a v a2s . c om if (n.getNodeType() == Node.TEXT_NODE) { Text t = (Text) n; sb.append(t.getData()); } } return sb.toString(); }
From source file:Main.java
private static void print(Node e, String tab) { if (e.getNodeType() == Node.TEXT_NODE) { System.out.println(tab + e.getNodeValue()); return;/* w ww .ja v a 2 s. c om*/ } System.out.print(tab + e.getNodeName()); NamedNodeMap as = e.getAttributes(); if (as != null && as.getLength() > 0) { System.out.print(" attributes=["); for (int i = 0; i < as.getLength(); i++) System.out.print((i == 0 ? "" : ", ") + as.item(i)); System.out.print("]"); } System.out.println(); if (e.getNodeValue() != null) System.out.println(tab + " " + e.getNodeValue()); NodeList childs = e.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) print(childs.item(i), tab + " "); }
From source file:Main.java
/** * /*w ww . ja v a 2 s .c o m*/ * @param e * @return */ public static String getText(Element e) { NodeList nl = e.getChildNodes(); int max = nl.getLength(); for (int i = 0; i < max; i++) { Node n = nl.item(i); if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue().trim(); } } return ""; }
From source file:Main.java
/** * (A useful utility method from IBM developerworks) *///from w ww . java 2s .c om public static String getTextContents(Node node) { NodeList childNodes; StringBuffer contents = new StringBuffer(); childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) { contents.append(childNodes.item(i).getNodeValue()); } } return contents.toString(); }
From source file:Main.java
/** * Get the text content of an element.//from w w w. j a v a 2s . c o m * * @param element * The element. * @param sbuf * The buffer to append to. * @param decend * Whether to descend into child elements. */ public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) { Node node = element.getFirstChild(); while (node != null) { switch (node.getNodeType()) { case Node.TEXT_NODE: sbuf.append(node.getNodeValue()); break; case Node.ELEMENT_NODE: if (decend) { getText((Element) node, sbuf, decend); } break; } node = node.getNextSibling(); } }
From source file:Main.java
private static Node getChildNodeByType(Element element, short nodeType) { if (element == null) { return null; }//from www . j a va 2 s . c o m NodeList nodes = element.getChildNodes(); if (nodes == null || nodes.getLength() < 1) { return null; } Node node; String data; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); short type = node.getNodeType(); if (type == nodeType) { if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { data = ((Text) node).getData(); if (data == null || data.trim().length() < 1) { continue; } } return node; } } return null; }
From source file:Main.java
/** * Static helper function to get the element data of the specified node. * /*from www . j av a 2s . c o m*/ * @param node * the node where the text data resides; may be <code>null</code> * in which case this funtion will return "" * * @return the complete text of the specified node, or an empty string if * the node has no text or is <code>null</code> */ public static String getElementData(final Node node) { StringBuffer ret = new StringBuffer(); if (node != null) { Node text; for (text = node.getFirstChild(); text != null; text = text.getNextSibling()) { /** * the item's value is in one or more text nodes which are its * immediate children */ if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) { ret.append(text.getNodeValue()); } else { if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) { ret.append(getElementData(text)); } } } } return ret.toString(); }
From source file:Main.java
/**Recursive method which has a side effect of setting the result to the pretty-printed flat text version of an XML node. * @param finger the current node//w w w . j a v a2 s.c o m * @param result the resultant string that is being built up. * @param indent the current indenting level. */ private static void doc2String(Node finger, StringBuffer result, int indent) { //Base Case for (int j = 0; j < indent; j++) result.append(' '); if (finger.getNodeType() == Node.TEXT_NODE) { result.append(finger.getNodeValue().trim()); result.append("\n"); return; } result.append('<'); //System.out.println("XMLUtils: appending " + finger.getNodeName() + " to output"); result.append(finger.getNodeName().trim()); result.append('>'); result.append("\n"); if (finger.hasChildNodes()) { NodeList childList = finger.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { doc2String(childList.item(i), result, indent + 1); } } for (int j = 0; j < indent; j++) result.append(' '); result.append("</"); //System.out.println("XMLUtils: appending end " + finger.getNodeName() + " to output"); result.append(finger.getNodeName().trim()); result.append('>'); result.append("\n"); }
From source file:Main.java
/** * Is the supplied W3C DOM Node a text node. * * @param node The node to be tested./* w w w . ja v a 2 s. c o m*/ * @return True if the node is a text node, otherwise false. */ public static boolean isTextNode(Node node) { short nodeType; if (node == null) { return false; } nodeType = node.getNodeType(); return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE; }
From source file:Main.java
public static String setInnerText(Element node, String value) { StringBuilder sb = new StringBuilder(); while (node.hasChildNodes()) { Node tn = node.getFirstChild(); if (tn.getNodeType() == Node.TEXT_NODE) { sb.append(tn.getNodeValue()); }/*from w w w .j a v a 2 s. c o m*/ node.removeChild(tn); } node.appendChild(node.getOwnerDocument().createTextNode(value)); return sb.toString(); }