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:Main.java

public static HashMap<String, String> getEleAttrNameValueMap(Element ele) {
    HashMap<String, String> ht = new HashMap<String, String>();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);//from  www. j ava2s.  c  o  m
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}

From source file:Main.java

/**
 *  Gets the node value as time./*from  w  w w.j a va  2  s. co  m*/
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsTime value
 *@exception  DOMException  Description of the Exception
 */
public final static long getNodeValueAsTime(Node node) throws DOMException {
    if (node == null)
        return -1;

    NamedNodeMap attrs = node.getAttributes();
    Node attr = attrs.getNamedItem("Unit");
    int factor = 1;
    String unit = attr.getNodeValue();
    if (unit.equals("sec")) {
        factor = 1000;
    } else if (unit.equals("min")) {
        factor = 60000;
    } else if (unit.equals("hr")) {
        factor = 3600000;
    } else if (unit.equals("day")) {
        factor = 86400000;
    }

    node = node.getFirstChild();
    if (node != null) {
        String time = node.getNodeValue().trim();
        return Integer.parseInt(time) * factor;
    }
    return -1;
}

From source file:Main.java

/**
 * Get the value of an element. If the node is a Document, use the
 * document element as the element node.
 * The value of an element node is the sum of all the element's
 * first generation child text nodes. Note that this is not what you
 * would get from a mixed element in an XSL program.
 * @param node the node.//from  ww w  .  j av  a 2s  .  co  m
 * @return the value of the element, or an empty string if the
 * node is not an element.
 */
public static String getElementValue(Node node) {
    if (node instanceof Document)
        node = ((Document) node).getDocumentElement();
    if (!(node instanceof Element))
        return "";
    NodeList nodeList = node.getChildNodes();
    String value = "";
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE)
            value += n.getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Get the text content of an element. If the element contains
 * mixed content (both elements and text), only the first text section
 * is returned.//from ww w.j a  v  a2s .  co m
 *
 * @param element target element to retrieve text on, cannot be null.
 * @return text content of the element.
 */
public static String getElementText(Element element) {
    element.normalize();
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            String s = n.getNodeValue();

            if (s != null) {
                return s.trim();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getAttribute(String attribute, Node node) {
    if (null != node.getAttributes()) {
        Node attributeNode = node.getAttributes().getNamedItem(attribute);

        if (attributeNode != null) {
            return attributeNode.getNodeValue().trim();
        }// w  w  w  . j a  v  a 2  s.  c  om
    }
    return null;
}

From source file:Main.java

/**
 *  Gets the node value as date.//from   www .  ja  v a  2 s .co m
 *
 *@param  node                          Description of the Parameter
 *@return                               The nodeValueAsDate value
 *@exception  DOMException              Description of the Exception
 *@exception  ParseException  Description of the Exception
 */
public final static Date getNodeValueAsDate(Node node) throws DOMException, ParseException {
    if (node == null)
        return null;

    NamedNodeMap attrs = node.getAttributes();
    Node attr = attrs.getNamedItem("DateTimeFormat");

    // Date format
    String format = attr.getNodeValue().trim();
    node = node.getFirstChild();
    if (node != null) {
        String date = node.getNodeValue().trim();
        DateFormat df = new SimpleDateFormat(format);
        return df.parse(date);
    }
    return null;
}

From source file:Main.java

/**
 * Returns the content of the specified node.
 * <p>//from www  .  j  a v a  2 s.c  o  m
 * Example:
 *
 * <pre>
 * {@code <name>John</name>
 * 
 * getContent(name); // returns "John"}
 * </pre>
 *
 * @param node
 *            The node to get the field child from.
 * @return The content of the specified node, or {@code null} if no such content exists.
 */
public static String getContent(Node node) {
    Node fieldNode = node.getFirstChild();
    if (fieldNode == null) {
        return null;
    }
    return fieldNode.getNodeValue();
}

From source file:Main.java

/**
 * Dumps a debug listing of the attributes of the given element to
 * System.out./*from  w w w .  j  ava 2 s.co  m*/
 * 
 * @param elem the element to dump the attributes of
 */
public static void dumpAttrs(Element elem) {
    System.out.println("Attributes of " + elem.getNodeName() + ", NS: " + elem.getNamespaceURI());
    NamedNodeMap attrs = elem.getAttributes();
    int len = attrs.getLength();
    for (int i = 0; i < len; ++i) {
        Node attr = attrs.item(i);
        System.out.println("  Name: " + attr.getNodeName() + ", Value: " + attr.getNodeValue() + ", NS: "
                + attr.getNamespaceURI());
    }
}

From source file:Main.java

/**
 *  Gets the node value as boolean./* w  w  w  .  ja va  2s  . c om*/
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsBoolean value
 *@exception  DOMException  Description of the Exception
 */
public final static boolean getNodeValueAsBoolean(Node node) throws DOMException {
    if (node != null) {
        node = node.getFirstChild();
        return node.getNodeValue() != null && (node.getNodeValue().equalsIgnoreCase("YES")
                || node.getNodeValue().equalsIgnoreCase("TRUE"));
    }

    return false;
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    NamedNodeMap namedNodeMap = node.getAttributes();
    if (namedNodeMap == null) {
        return null;
    }/*from  w w w  .  j a va  2  s .  com*/
    Node attrNode = namedNodeMap.getNamedItem(name);
    if (attrNode == null) {
        return null;
    }
    return attrNode.getNodeValue();
}