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

private static void print(Node e, String tab) {

    if (e.getNodeType() == Node.TEXT_NODE) {
        System.out.println(tab + e.getNodeValue());
        return;/*from   w  ww.  ja  v a 2  s . c  om*/
    }

    System.out.print(tab + e.getNodeName());

    NamedNodeMap as = e.getAttributes();
    if (as != null && as.getLength() > 0) {
        System.out.print(" attributes=[");
        for (int i = 0; i < as.getLength(); i++)
            System.out.print((i == 0 ? "" : ", ") + as.item(i));
        System.out.print("]");
    }
    System.out.println();

    if (e.getNodeValue() != null)
        System.out.println(tab + " " + e.getNodeValue());

    NodeList childs = e.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++)
        print(childs.item(i), tab + " ");
}

From source file:Main.java

/**
 * This method return role name from xml file.
 * @param element String- privilege Element
 * @param elementName -Element name for which value has to be return
 * @return String Role name/*  w w  w  .j  ava 2  s. c o  m*/
 */
public static String getElementValue(Element element, String elementName) {
    String roleName = "";
    NodeList elementList = element.getElementsByTagName(elementName);
    Element ele = (Element) elementList.item(0);
    NodeList valueNodeList = ele.getChildNodes();
    Node node = ((Node) valueNodeList.item(0));
    if (node != null) {
        roleName = node.getNodeValue();
    }
    return roleName;
}

From source file:Main.java

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();
        }//from   w w w  . ja v  a2 s .com
    }
    return "";
}

From source file:Main.java

public static String readTextNode(Node item) {
    StringBuffer buf = new StringBuffer(""); //$NON-NLS-1$
    NodeList children2 = item.getChildNodes();
    if (children2 != null) {
        for (int k = 0; k < children2.getLength(); k++) {
            Node item2 = children2.item(k);
            if (item2 != null) {
                buf.append(item2.getNodeValue());
            }/*www  .  j a v  a2  s .  com*/
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getNodeAttributeValue(String attrName, Node node) {
    if (node == null || attrName == null) {
        return null;
    }//from   w  w w  . ja v  a 2  s  .c  o  m

    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null) {
        return null;
    }

    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode != null) {
        return attrNode.getNodeValue();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get the value of a node assuming there is one child node with a text value
 * /*w ww. j  av a  2 s .  c  o  m*/
 * @param node
 * @return the value
 */
public static String getNodeTextValue(Node node) {
    String value = null;
    if (node.hasChildNodes()) {
        Node text = node.getFirstChild();
        value = text.getNodeValue();
    }
    return replaceSpecialCharacters(value);
}

From source file:Main.java

/**
 * Get string value for a tag//from   w  w  w . j  a v  a 2  s . c  o  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 getStringValueForXMLTag(Element xmlElement, String key) {
    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

/**
 * Extracts the text from the given element.
 * Element.getTextContet() is java5 specific, so we need to use this until we drop 1.4 support.
 *///  w ww.  j  a v  a  2 s .  c  o  m
public static String getTextContent(Node element) {
    StringBuffer text = new StringBuffer();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child instanceof Text) {
            text.append(child.getNodeValue());
        }
    }

    return text.toString();
}

From source file:Main.java

public static String getAttribute(Node node, String attributeName) {
    Node attribute = node.getAttributes().getNamedItem(attributeName);
    if (attribute != null) {
        return attribute.getNodeValue();
    }//from w  w  w.j a v  a  2  s .c  o m
    return null;
}

From source file:Main.java

static String attributeOrNull(Node node, String... attributes) {
    for (int i = 0; i < attributes.length; i++) {
        String attribute = attributes[i];
        Node attr = node.getAttributes().getNamedItem(attribute);
        if (attr != null)
            return attr.getNodeValue();
    }//w  ww. j av  a 2 s  . c o  m
    return null;
}