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

/**
 *  Gets the node value as float./*  w ww.j a  va 2s .c o m*/
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsFloat value
 *@exception  DOMException  Description of the Exception
 */
public final static float getNodeValueAsFloat(Node node) throws DOMException {
    if (node != null) {
        node = node.getFirstChild();
        if (node != null)
            return Float.parseFloat(node.getNodeValue());
    }
    return -1;
}

From source file:Main.java

/**
 * Copy elements from one document to another attaching at the specified
 * element and translating the namespace.
 *
 * @param from         copy the children of this element (exclusive)
 * @param to           where to attach the copied elements
 * @param newNamespace destination namespace
 *
 * @since 8.4/* w  w w .ja v a2  s  . com*/
 */
public static void copyDocument(Element from, Element to, String newNamespace) {
    Document doc = to.getOwnerDocument();
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode = null;
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element oldElement = (Element) node;
            newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
            NamedNodeMap m = oldElement.getAttributes();
            Element newElement = (Element) newNode;
            for (int index = 0; index < m.getLength(); index++) {
                Node attr = m.item(index);
                newElement.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            copyDocument(oldElement, newElement, newNamespace);
        } else {
            newNode = node.cloneNode(true);
            newNode = to.getOwnerDocument().importNode(newNode, true);
        }
        if (newNode != null) {
            to.appendChild(newNode);
        }
    }
}

From source file:Main.java

/**
 *  Gets the node value as int./*from   w  w w .  j a  v  a 2 s.c o  m*/
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsInt value
 *@exception  DOMException  Description of the Exception
 */
public final static int getNodeValueAsInt(Node node) throws DOMException {
    if (node != null) {
        node = node.getFirstChild();
        if (node != null)
            return Integer.parseInt(node.getNodeValue());
    }
    return -1;
}

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.err.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());
    }/*from  ww w  . j  a  va  2 s .co  m*/
}

From source file:Main.java

/**
 * Finds a node in a document and returns the value from an attribute of
 * this node. If there are multiple nodes with the specified tag name, the
 * attribute value of the first node found will be returned.
 * //from  w ww .  jav a 2 s. c  o  m
 * @param document
 *            the document.
 * @param nodeName
 *            the name of the node .
 * @param attributeName
 *            the name of the node's attribute.
 * @return the value of the node's attribute or <code>null</code> if either
 *         no node with the specified tag name or no attribute with the
 *         specified attribute name could be found.
 */
public static String getAttributeValueFromNode(Document document, String nodeName, String attributeName) {
    Node node = getNode(document, nodeName);
    if (node != null) {
        Node attribute = getAttributeValue(node, attributeName);
        if (attribute != null) {
            return attribute.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

private static String readXsdVersionFromFile(Document doc) {
    final String JBOSS_ESB = "jbossesb";
    NodeList nodes = doc.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (JBOSS_ESB.equals(node.getNodeName())) {
                NamedNodeMap attributes = node.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node attribute = attributes.item(j);
                    if ("xmlns".equals(attribute.getNodeName())) {
                        String value = attribute.getNodeValue();
                        if (value.contains(JBOSS_ESB) && value.endsWith(".xsd"))
                            return value.substring(value.lastIndexOf('/') + 1, value.length());
                        else
                            throw new IllegalStateException(
                                    "The ESB descriptor points to an invalid XSD" + value);
                    }//from w  ww .  j  ava2 s  . c o  m
                }
            }
        }
        throw new IllegalArgumentException("No root node " + JBOSS_ESB + " found.");
    } else
        throw new IllegalArgumentException("Descriptor has no root element !");
}

From source file:Main.java

/**
 * Return the text (node value) of the first node under this, works best if
 * normalized.//from   w ww  .j  av a2  s  . co  m
 */
public static String elementValue(Element element) {
    if (element == null)
        return null;
    // make sure we get all the text there...
    element.normalize();
    Node textNode = element.getFirstChild();

    if (textNode == null)
        return null;
    // should be of type text
    return textNode.getNodeValue();
}

From source file:Main.java

/**
 *  Gets the node value as double.//from w  w  w .j  ava2 s  .  c  om
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsDouble value
 *@exception  DOMException  Description of the Exception
 */
public final static double getNodeValueAsDouble(Node node) throws DOMException {
    if (node != null) {
        node = node.getFirstChild();
        if (node != null)
            return Double.parseDouble(node.getNodeValue());
    }
    return -1;
}

From source file:Main.java

private static boolean isEmptyTextNode(Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        String str = node.getNodeValue();
        if (str == null || str.trim().length() == 0) {
            return true;
        }//  w ww  . jav a  2s.  com
    }
    return false;
}

From source file:Main.java

public static String parseNodeAsText(Node node) {
    if (node == null)
        return null;
    String text = null;//from  ww  w.  j ava2  s  . c o m
    if (node.getNodeType() == 3) {
        text = node.getNodeValue();
    } else {
        StringBuffer sb = new StringBuffer();
        NodeList children = node.getChildNodes();
        for (int i = 0; children != null && i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child != null) {
                int childType = child.getNodeType();
                if (childType == 3 || childType == 4 || childType == 8) {
                    String childText = child.getNodeValue();
                    sb.append(childText);
                } else {
                    sb.append(parseNodeAsText(child));
                }
            }
        }

        if (sb.length() > 0)
            text = sb.toString();
    }
    return text;
}