List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/** * Gets the first child element of an element * * @param element the parent/* ww w . java2s.c om*/ * @return the first child element or null if there isn't one */ public static Element getFirstChildElement(Element element) { Node child = element.getFirstChild(); while (child != null && child.getNodeType() != Node.ELEMENT_NODE) child = child.getNextSibling(); return (Element) child; }
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()); }/*ww w .j ava 2 s. co m*/ node.removeChild(tn); } node.appendChild(node.getOwnerDocument().createTextNode(value)); return sb.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 {/* w ww .j ava 2 s .c om*/ if (n.getNodeType() == Node.TEXT_NODE) { return n.getNodeValue(); } } while ((n = n.getNextSibling()) != null); } } return node.getNodeValue(); }
From source file:Main.java
public static String getContentText(Element elementNode) { StringBuffer result = new StringBuffer(); NodeList children = elementNode.getChildNodes(); for (int c = 0; c < children.getLength(); c++) { Node child = children.item(c); if ((child.getNodeType() == Node.TEXT_NODE) && (child instanceof Text)) { String name = ((Text) child).getData(); result.append(name);//from ww w . j av a 2 s.c om } } return result.toString(); }
From source file:Main.java
/** * Get element node string value.//from ww w . j av a 2s.c om * * @param element element to get string value for * @return concatenated text node descendant values */ public static String getStringValue(final Element element) { final StringBuilder buf = new StringBuilder(); final NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node n = children.item(i); switch (n.getNodeType()) { case Node.TEXT_NODE: buf.append(n.getNodeValue()); break; case Node.ELEMENT_NODE: buf.append(getStringValue((Element) n)); break; } } return buf.toString(); }
From source file:Main.java
/** * Gets the String value of the node. //w ww. j a v a2 s . co m If the node does not contain text then an empty String is returned * @param node the node of interest * @return the value of that node */ public static String getTextForNode(Node node) { NodeList children = node.getChildNodes(); if (children == null) { return ""; } for (int i = 0; i < children.getLength(); i++) { Node childNode = children.item(i); if ((childNode.getNodeType() == Node.TEXT_NODE) || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) { return childNode.getNodeValue(); } } return ""; }
From source file:Main.java
public static List<Element> getChildElementsByTagName(Element element, String name) { List<Element> elements = new LinkedList<Element>(); NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) node; if (e.getTagName().equals(name)) { elements.add(e);// ww w . j ava2s. c om } } } return elements; }
From source file:Main.java
public static List<Node> getAllChildNodes(Node node) { if (node == null) return null; List<Node> result = new ArrayList<Node>(); NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) { Node curnode = nodelist.item(i); int type = curnode.getNodeType(); if (type != Node.TEXT_NODE) result.add(nodelist.item(i)); List<Node> childlist = getAllChildNodes(curnode); if (childlist != null) result.addAll(childlist);/* w w w . j a v a2 s . c o m*/ } return result; }
From source file:Main.java
/** * /*from www. ja v a 2 s . c o m*/ * @return one large string with the contents of all TextNodes, or null if * there are non text nodes or no text nodes as children. */ public static String getContentsOfTextOnlyNode(Node n) { NodeList children = n.getChildNodes(); if (children.getLength() == 0) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() == Node.TEXT_NODE) { sb.append(node.getNodeValue()); } else { return null; } } return sb.toString(); }
From source file:Main.java
/** * Returns all children of the given element which are element named as * specified.// ww w .jav a 2 s . c o m */ public static List<Element> getNamedChildren(Element element, String elementNames) { List<Element> result = new ArrayList<Element>(); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(elementNames)) { result.add((Element) node); } } return result; }