Example usage for org.w3c.dom Node getNodeValue

List of usage examples for org.w3c.dom Node getNodeValue

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeValue.

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:XMLUtils.java

/**
 * Returns the value of the attribute of the given element.
 * @param base the element from where to search.
 * @param name of the attribute to get./* ww w  .  ja v  a2  s . c  o m*/
 * @return the value of this element.
 */
public static String getAttributeValue(final Element base, final String name) {

    // get attribute of this element...
    NamedNodeMap mapAttributes = base.getAttributes();
    Node node = mapAttributes.getNamedItem(name);
    if (node != null) {
        return node.getNodeValue();
    }
    return null;
}

From source file:Main.java

public static int getAttributeIntValue(Node n, String item, int dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }/* w  ww .ja v  a 2 s.  c o  m*/
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Integer.parseInt(val);
}

From source file:Main.java

/**
 * Gets the value from the first descendant of a given ancestor, where the
 * descendant has the specified name.//  w  ww.j  a v a 2s  .  c o m
 * 
 * @param ancestor
 *            the ancestor element
 * @param tagname
 *            the tag name of the descendant to find.
 * @return the value of the descendant or <code>null</code> if no descendant
 *         with that name could be found or the descendant found has no
 *         value.
 */
public static String getValueFromDescendant(Element ancestor, String tagname) {
    if (ancestor != null) {
        NodeList nl = ancestor.getElementsByTagName(tagname);
        if (!isEmpty(nl)) {
            Element e = (Element) nl.item(0);
            Node c = e.getFirstChild();
            if (c != null) {
                return c.getNodeValue();
            }
        }
    }
    return null;
}

From source file:Main.java

public static boolean getAttributeBooleanValue(Node n, String item, boolean dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null) {
        return dflt;
    }/* w  w  w  .  j  ava  2  s .  c  om*/
    final String val = d.getNodeValue();
    if (val == null) {
        return dflt;
    }
    return Boolean.parseBoolean(val);
}

From source file:Main.java

public static String getElementValue(Element parent) {

    NodeList nodes = parent.getChildNodes();
    int current = 0;
    int length = nodes.getLength();
    while (current < length) {
        Node node = nodes.item(current);
        if (node instanceof Text) {
            String value = node.getNodeValue();
            if (value != null) {
                return value.trim();
            }//from   w w  w. ja v a  2s . com
        }
        current++;
    }
    return "";
}

From source file:Main.java

/**
 * @param node/* w w  w. jav a  2s .  co m*/
 * @param attributeKey
 * @return
 */
public static String getAttributeValue(Node node, String attributeKey) {
    NamedNodeMap attributes = node.getAttributes();
    Node attribute = attributes.getNamedItem(attributeKey);
    if (attribute == null) {
        return null;
    }
    return attribute.getNodeValue();
}

From source file:Main.java

/**
 * Retrieves the value for a xml Node attribute or a default if not found.
 * /*from w  w w . j  a  v a2  s .c om*/
 * @param node The Node to fetch the value from.
 * @param name The attribute name.
 * @param defaultValue The default value to return if not found.
 * @return and attribute value or a default if not found.
 */
public static String getNodeAttributeOrDefault(Node node, String name, String defaultValue) {
    NamedNodeMap attributes = node.getAttributes();
    Node valueNode = attributes.getNamedItem(name);
    String value = defaultValue;
    if (valueNode != null)
        value = valueNode.getNodeValue();
    return value;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attributeName) {
    for (Node child : getChildren(node)) {
        if (child.getNodeName().equalsIgnoreCase(attributeName)) {
            return child.getNodeValue();
        }/*from   w ww .j a  v a2  s.c  om*/
    }
    return null;
}

From source file:Main.java

public static String getElementCDATAByTagName(Element current, String name, String namespace) {

    Element e = getElementByTagName(current, name, namespace);

    if (e == null)
        return null;

    Node child = e.getFirstChild();

    if (child == null)
        return null;

    return child.getNodeValue();

}

From source file:Main.java

/**
 * Get the element text value.  Locate the text node and return
 * its contents.//from   www .j  a  v a  2  s. c om
 *
 * @param element The element to get the name for
 * @return The text value
 */
public static String getElementTextValue(Element element) {
    String value = "";

    // get the text node
    Node node = getElementTextNode(element);
    if (node != null) {
        value = node.getNodeValue();
    }

    // if the entire string is whitespace ignore this
    // but don't trim the whitespace if it is there
    String testValue = value.trim();
    if (testValue.length() <= 0) {
        value = "";
    }
    return (value);
}