List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
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 w w w . j a v a2 s. c o 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
/** * Extracts an attribute from a node.//from w w w .j av a 2 s . co m * * @param node * @param attr * @param def * @return The value of the attribute, or def */ public static String getAttribute(Node node, String attr, String def) { NamedNodeMap attrs = node.getAttributes(); Node val = attrs.getNamedItem(attr); if (val != null) { return val.getNodeValue(); } return def; }
From source file:Main.java
/** * Get all attributes as a Map from a node * * @param node the node taht contains the attributes to read * @return the values or a empty map/* w w w. j a va2 s . co m*/ */ public static HashMap<String, String> getAttributes(Node node) { final HashMap<String, String> result = new HashMap<>(); final NamedNodeMap atts = node.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Node att = atts.item(i); result.put(att.getNodeName(), att.getNodeValue()); } return result; }
From source file:Main.java
public static String getElementValue(final Node sourceNode) { String result = null;//from www . ja v a2 s .co m if (sourceNode == null) { return result; } if (sourceNode.getNodeType() == Node.ELEMENT_NODE) { Node firstChild = sourceNode.getFirstChild(); if (firstChild != null) { result = firstChild.getNodeValue(); } } return result; }
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 ww . ja v a 2 s.c o m*/ node.removeChild(tn); } node.appendChild(node.getOwnerDocument().createTextNode(value)); return sb.toString(); }
From source file:Main.java
public static String getAttributeValue(Node node, String attribute) { NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { Node nameAttr = attributes.getNamedItem(attribute); if (nameAttr != null) { return nameAttr.getNodeValue(); }/*from w w w . j a v a 2 s . com*/ } return null; }
From source file:Main.java
static public String getNodeValue(Element parent, String nodeName) { NodeList nodes = parent.getElementsByTagName(nodeName); if (nodes.getLength() == 0) throw new IllegalArgumentException("Cannot find node " + nodeName); Node textNode = nodes.item(0).getFirstChild(); if (textNode != null) return (textNode.getNodeValue()); else//from ww w . ja v a 2s . com return (""); }
From source file:Main.java
public static String getNodeAttributeValue(Node node, String attrName) { NamedNodeMap attrs = node.getAttributes(); if (attrs == null) return (null); Node value = attrs.getNamedItem(attrName); if (value == null) return (null); return (value.getNodeValue()); }
From source file:Main.java
public static String getAttributeFromNode(Node node, String attributeName) { if (node == null || attributeName == null) return null; NamedNodeMap attributes = node.getAttributes(); Node attribute = attributes.getNamedItem(attributeName); String value = null;/*from w w w .j av a 2 s . co m*/ if (attribute != null) value = attribute.getNodeValue(); return value; }
From source file:Main.java
public static String getBody(Element element) { NodeList nodes = element.getChildNodes(); if (nodes == null || nodes.getLength() == 0) { return null; }// ww w .j a v a2 s . co m Node firstNode = nodes.item(0); if (firstNode == null) { return null; } return firstNode.getNodeValue(); }