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

/**
 * Utility method to fetch the attribute value from the given 
 * element node//from   w  ww  . j a v  a2 s  . co  m
 * @param sNode
 * @param attribName
 * @return
 */
public static String getAttributeValue(Node sNode, String attribName) {
    String value = null;
    NamedNodeMap attrs = sNode.getAttributes();
    if (attrs != null) {
        Node attr = attrs.getNamedItem(attribName);
        if (attr != null) {
            value = attr.getNodeValue();
        }
    }
    return value;
}

From source file:Main.java

public static String getAttributeValue(Node n, String item) {
    final Node d = n.getAttributes().getNamedItem(item);
    if (d == null)
        return "";
    final String val = d.getNodeValue();
    if (val == null)
        return "";
    return val;
}

From source file:Main.java

public static String getTextValue(Element e) {
    NodeList childs = e.getChildNodes();
    for (int k = 0; k < childs.getLength(); k++) {
        Node n = childs.item(k);
        if (n.getNodeType() == 3) {
            return n.getNodeValue();
        }//from   w w w .j  av a  2 s .  c  o  m
    }
    return null;
}

From source file:Main.java

static public String getStringValue(Element el, boolean trimWhitespace) {
    StringBuilder sb = new StringBuilder(1024);
    String str;/*from  w  ww  .j  a va  2 s .co m*/

    NodeList nl = el.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (n instanceof Text) {
            sb.append(n.getNodeValue());
        }
    }
    if (trimWhitespace) {
        str = sb.toString().trim();
    } else {
        str = sb.toString();
    }
    return str;

}

From source file:Main.java

public static String getTextTag(Node node) {
    Node child = node.getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*  w  ww  .j a  v a  2  s  .co  m*/
}

From source file:Main.java

/**
 * Gets the text value from the specified element.
 * @param e The element to get the text content from.
 * @return The text value of the element, or null if the tag is empty.
 *//*w w w .j  ava2  s.c om*/
private static String getTagValue(Element e) {
    Node n = (Node) e.getChildNodes().item(0);
    if (n == null)
        return null;

    return n.getNodeValue();
}

From source file:Main.java

public static String[] getStringValuesForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);

    if (nl.getLength() > 0) {

        String[] output = new String[nl.getLength()];

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i).getFirstChild();
            if (node != null) {
                output[i] = node.getNodeValue().trim();
            }/*from  w  w  w.  j  a  v a2s . co m*/
        }
        return output;
    }
    return null;
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name.//from  w w  w  .  jav  a 2 s.  co  m
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:Main.java

public static String elementToString(Element e) {
    StringBuilder buf = new StringBuilder();
    buf.append(e.getTagName()).append("{");
    for (int i = 0; i < e.getAttributes().getLength(); i++) {
        if (i > 0) {
            buf.append(", ");
        }//ww w  . j  a  va2s  . c  om
        Node n = e.getAttributes().item(i);
        buf.append(attributeName((Attr) n)).append("=").append(n.getNodeValue());
    }
    buf.append("}");
    return buf.toString();
}

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