List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:Main.java
/** * Retrieves the value for a xml Node attribute or fails if not found. * /*from www. j a v a2s .c o m*/ * @param <T> the exception type to throw * @param node The Node to fetch the value from. * @param name The attribute name. * @param e an Exception instance * @return the attribute value * @throws T */ public static <T extends Exception> String getNodeAttributeOrFail(Node node, String name, T e) throws T { NamedNodeMap attributes = node.getAttributes(); Node valueNode = attributes.getNamedItem(name); if (valueNode == null) throw e; return valueNode.getNodeValue(); }
From source file:Main.java
/** * Get first value (value of first child) from the specified Element.<br> * If no value found 'def' value is returned. *///w ww . j av a2s. c o m public static String getFirstValue(Element element, String def) { if (element != null) { final Node child = element.getFirstChild(); if (child != null) return child.getNodeValue(); } return def; }
From source file:Main.java
public static String getAttributeValue(Node node, String attributeName) { NamedNodeMap attributes = node.getAttributes(); if (null != attributes) { Node namedItem = attributes.getNamedItem(attributeName); if (null != namedItem) { return namedItem.getNodeValue(); }//from w ww .jav a2 s . com } return ""; }
From source file:Main.java
/** * Get the Value of a node that has an embedded text node as a child * @param doc the Document to search for the node name * @param nodeName the name of the node to get the value * @return the value of the text node child *///ww w . j av a 2s . c om public static String getNodeTextValue(Document doc, String nodeName) { String value = null; NodeList nodes = doc.getElementsByTagName(nodeName); if (nodes.getLength() > 0) { Node node = nodes.item(0); if (node.hasChildNodes()) { Node text = node.getFirstChild(); value = text.getNodeValue(); } } return replaceSpecialCharacters(value); }
From source file:Main.java
public static String getExperimentNameFromLineNode(Node linenode) { try {/* www. j ava 2s. c om*/ Node t1 = linenode.getAttributes().getNamedItem("experimentname"); if (t1 != null) return t1.getNodeValue(); else return linenode.getOwnerDocument().getElementsByTagName("experimentname").item(0).getFirstChild() .getNodeValue(); } catch (NullPointerException npe) { return ""; } }
From source file:Main.java
/*** * Print every attribute and value of a node, one line at a time. * If {@link entry} is null, then outputs "<null/>". * * @param outs where to send output, cannot be null. * @param entry XML node to examine, OK if null. *///from w ww. java2 s .co m public static void XmlPrintAttrs(PrintStream outs, Node entry) { if (entry == null) { outs.print("<null/>"); return; } // see http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/NamedNodeMap.html NamedNodeMap attrs = entry.getAttributes(); for (int k = attrs.getLength(); --k >= 0;) { Node n = attrs.item(k); outs.printf("+++ has attr %s = %s\n", n.getNodeName(), n.getNodeValue()); } }
From source file:Main.java
/** * /* www .j a v a 2s .com*/ * @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
/** * This will get the text value of an element. * * @param node The node to get the text value for. * @return The text of the node.//from ww w .j a va 2s . com */ public static String getNodeValue(Element node) { String retval = ""; NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node next = children.item(i); if (next instanceof Text) { retval = next.getNodeValue(); } } return retval; }
From source file:Main.java
/** * Gets the value of a named attribute of a node. * @param node Node The context node./*from ww w .j av a 2 s . co m*/ * @param name String * @return String null means node is not found. */ public static String getAttributeString(Node node, String name) { Node nd = findAttribute(node, name); if (nd != null) return nd.getNodeValue(); return null; }
From source file:Main.java
public static String getDirectTextContent(Node aNode) { String result = ""; NodeList nodeList = aNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node aSubNode = nodeList.item(i); if (aSubNode.getNodeType() == Node.TEXT_NODE) { result += aSubNode.getNodeValue(); }//from w w w. j a v a 2 s. c om } return result; }