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 long getAttributeLongValue(Node n, String item, long dflt) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null)
        return dflt;
    final String val = d.getNodeValue();
    if (val == null)
        return dflt;
    return Long.parseLong(val);
}

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;
    final String val = d.getNodeValue();
    if (val == null)
        return dflt;
    return Boolean.parseBoolean(val);
}

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 w ww .j av a2  s.c  om*/
 */
public static String getNodeValue(Element node) {
    StringBuilder sb = new StringBuilder();
    NodeList children = node.getChildNodes();
    int numNodes = children.getLength();
    for (int i = 0; i < numNodes; i++) {
        Node next = children.item(i);
        if (next instanceof Text) {
            sb.append(next.getNodeValue());
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Get string value for a tag/*from   w w  w.ja  va 2  s  .  co  m*/
 * 
 * @param xmlElement
 *            Root Element of subtree in which required tag is to be
 *            searched
 * @param key
 *            Required tage name
 * @return String value
 */
public static String getStringValueForXMLTagFromAnywhere(Element xmlElement, String key) {

    // Start - testing
    String value = xmlElement.getAttribute(key);
    System.out.println("Value is - " + value);
    // End - testing

    NodeList nl = xmlElement.getElementsByTagName(key);
    String output = "nothing";
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            output = node.getNodeValue().trim();
            return output;
        }
    }
    return output;
}

From source file:Main.java

public static void getNamespaces(Node node, Map<String, String> list) {
    NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node n = atts.item(i);
        if ("xmlns".equals(n.getNodeName())) {
            list.put(n.getNodeName(), n.getNodeValue());
        } else {//  ww w  . j a  v  a  2  s.c  o m
            if (n.getNodeName().startsWith("xmlns:")) {
                list.put(n.getNodeName().substring(6), n.getNodeValue());
            }
        }
    }
}

From source file:Main.java

/**
 * /*  ww  w  .  j av a2 s  .c  o  m*/
 * @param element
 * @param tagname
 * @param valueRange
 * @return List
 */
public static List<String> getDOMElementTextByTagName(final Element element, String tagname,
        final int valueRange) {
    final List<String> list = new ArrayList<>();

    if (element != null) {
        // Just to ensure we pick off by the name of the child node...
        final NodeList nodeList = element.getElementsByTagName(tagname.substring(tagname.lastIndexOf("/") + 1)); //$NON-NLS-1$

        if (nodeList.getLength() == 0) {
            list.add(null);
        }

        for (int i = 0; i < ((valueRange == ALL_VALUES) ? nodeList.getLength() : 1); i++) {
            final Node node = nodeList.item(i);

            if (node != null) {
                final Node text = node.getFirstChild();
                list.add(text.getNodeValue());
            }
        }
    } else {
        list.add(null);
    }

    return list;
}

From source file:Main.java

public static String getAttributeValue(Element start, String attrName) {
    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            if (attrName.equals(attr.getNodeName().trim()))
                return attr.getNodeValue().trim();
        }/*w  ww .  j a  v a2  s.  c o m*/
        return null;
    } else
        return null;
}

From source file:Main.java

/**
 *
 * @param node Node// w w w  .ja  v a 2 s.c  o m
 * @param nodeName String
 * @param errValue String
 * @return String
 */
public static String getChildStringByName(Node node, String nodeName, String errValue) {
    Node nd = getChildByName(node, nodeName);
    if (nd != null) {
        Node fnd = nd.getFirstChild();
        if (fnd != null)
            return fnd.getNodeValue();
    }
    return errValue;
}

From source file:Main.java

public static String getText(Element element) {
    StringBuffer buf = new StringBuffer();
    NodeList list = element.getChildNodes();
    boolean found = false;
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            buf.append(node.getNodeValue());
            found = true;//  ww w. j a va  2 s. c om
        }
    }
    return found ? buf.toString() : null;
}

From source file:Main.java

public static String getValue(Element item, String tag) {
    String textValue = null;// ww  w.j  a  v a2  s  . c o  m

    NodeList nodes = item.getElementsByTagName(tag);
    if (nodes != null && nodes.getLength() > 0) {
        Element element = (Element) nodes.item(0);

        Node node = element.getFirstChild();

        if (node != null) {
            textValue = node.getNodeValue();
        }
    }

    return textValue;
}