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

/**
 * Prints variables of TEXT nodes.//from w ww . j a  v a2  s. c om
 * 
 * @param node a parent node.
 * @param level a number of indent.
 */
private static void printTextNode(Node node, int level) {
    String tmp = node.getNodeValue();
    tmp = tmp.trim();
    if (tmp.length() == 0) {
        return;
    }
    for (int i = 0; i < level; i++) {
        System.out.print("  ");
    }
    System.out.println("[" + tmp + "]");
}

From source file:Main.java

public static String getAttributeValue(Node elementNode, String attributeName) {
    if (elementNode == null) {
        return null;
    }/*from  ww  w . j av  a  2 s .  c  o m*/
    NamedNodeMap attributes = elementNode.getAttributes();
    if (attributes == null) {
        return null;
    }
    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute == null) {
        return null;
    }
    return attribute.getNodeValue();
}

From source file:Main.java

public static void removeAttribute(Node parent, String name, String value, boolean recursive) {
    NamedNodeMap nnm = parent.getAttributes();
    if (nnm != null) {
        if (value == null) {
            nnm.removeNamedItem(name);//from ww  w  .  ja v  a 2 s. co m
        } else {
            Node attr = nnm.getNamedItem(name);
            if (attr != null) {
                String attrVal = attr.getNodeValue();
                if (value.equals(attrVal)) {
                    nnm.removeNamedItem(name);
                }
            }
        }
    }
    if (recursive) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            removeAttribute(child, name, value, recursive);
        }
    }
}

From source file:XMLUtils.java

/**
 * Extract all text children of an element
 *//*  www  . j a  v  a2s  . com*/
public static String extractTextChildren(Element parentNode) {
    NodeList childNodes = parentNode.getChildNodes();
    String result = new String();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result += node.getNodeValue();
        }
    }
    return result;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    if (node == null) {
        return null;
    } else if (node instanceof Text) {
        return node.getNodeValue().trim();
    } else if (node instanceof Element) {
        node.normalize();/* w ww  . j av  a  2  s  . c o  m*/
        Node temp = node.getFirstChild();
        if (temp != null && (temp instanceof Text))
            return temp.getNodeValue().trim();
        else
            return "";
    } else {
        return node.getNodeValue().trim();
    }
}

From source file:Main.java

/**
 * @param node/*from  w  ww  .  j  ava  2  s .  com*/
 * @param attributeName
 * @return the String value of the given attributeName for the given node. null if the attributeName is not found or the input node is null.
 */
public static String nullSafeGetStringAttribute(Node node, String attributeName) {
    if (node != null) {
        Node namedItem = node.getAttributes().getNamedItem(attributeName);
        if (namedItem != null) {
            return namedItem.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attributeName) {
    if (node == null || attributeName == null) {
        return null;
    }//from  w ww  . j  av a2  s  . co  m

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

    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute != null) {
        return attribute.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4// w  w w.j  av a 2s. co m
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

From source file:Main.java

/**
 * Returns the string value of the named attribute for the given node
 * If no such attribute exists, returns an empty string
 *//*from  w  ww .  j av a 2  s . c  o m*/
public static String getStrValue(Node node, String name) {
    // Look for the attribute
    Node att = get_named_attribute(node, name);
    if (att != null) {
        // Return the value
        return att.getNodeValue();
    } else {
        // No such attribute
        return "";
    }
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }/*from  w w w.  j  ava2  s  .c  o m*/
            }
        }
    }
    return "";
}