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:eu.planets_project.services.utils.cli.CliMigrationPaths.java

private static URI decodeURI(Node uri) throws URISyntaxException {
    NamedNodeMap attrs = uri.getAttributes();

    Node item = attrs.getNamedItem("value");
    String urivalue = item.getNodeValue();
    return new URI(urivalue);
}

From source file:XmlUtil.java

/**
 * Returns value of first available child text node or <code>null</code> if not found.
 *//*from   w  w  w .j a  va 2  s . c  o m*/
public static String getFirstChildTextNodeValue(Node node) {
    NodeList children = node.getChildNodes();
    int len = children.getLength();
    for (int i = 0; i < len; i++) {
        Node n = children.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * returns contents of first TextNode defined within an XML Node
 * //from ww  w. ja  v a  2  s.co m
 * @param xmlNode parsed XML Node in which to look for a TextNode
 * @return text character string value of text node
 **/
public static String xmlFindTextNode(final Node xmlNode) {
    if (xmlNode == null) {
        return null;
    }

    final NodeList children = xmlNode.getChildNodes();
    final int childrenCnt = size(children);
    for (int j = 0; j < childrenCnt; j++) {
        final Node childNode = children.item(j);
        if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
            return childNode.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static void printTree(Node doc) {
    if (doc == null) {
        System.out.println("Nothing to print!!");
        return;//w w  w. ja va2  s .  co  m
    }
    try {
        System.out.println(doc.getNodeName() + "  " + doc.getNodeValue());
        NamedNodeMap cl = doc.getAttributes();
        for (int i = 0; i < cl.getLength(); i++) {
            Node node = cl.item(i);
            System.out.println("\t" + node.getNodeName() + " ->" + node.getNodeValue());
        }
        NodeList nl = doc.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            printTree(node);
        }
    } catch (Throwable e) {
        System.out.println("Cannot print!! " + e.getMessage());
    }
}

From source file:Main.java

/**
 * Remove empty CDATA sections from the given node and all of its descendants. Some parsers
 * silently discard empty CDATA sections, and this method may be used to compare the output from
 * parsers that behave differently with respect to empty CDATA sections.
 * /*w w w . j  a  va 2 s .c om*/
 * @param node
 *            the node to process
 */
public static void removeEmptyCDATASections(Node node) {
    Node child = node.getFirstChild();
    while (child != null) {
        Node next = child.getNextSibling();
        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            if (child.getNodeValue().length() == 0) {
                child.getParentNode().removeChild(child);
            }
            break;
        case Node.ELEMENT_NODE:
            removeEmptyCDATASections(child);
        }
        child = next;
    }
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes and collects and returns
 * their values./*from  w ww. ja va  2  s  . com*/
 *
 * @param node     The parent node
 * @param elemName The matching child node element name
 * @return List of node values
 */
public static Enumeration getChildrenNodeValues(Node node, String elemName) {
    Vector vect = new Vector();
    NodeList nl = node.getChildNodes();
    int n = nl.getLength();
    for (int i = 0; i < n; i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(elemName)) {
            Node child = nd.getFirstChild();
            if (child == null) {
                vect.add("");
            } else {
                vect.add(child.getNodeValue());
            }
        }
    }
    return vect.elements();
}

From source file:DocWriter.java

private static String nodeWithAttrs(Node node, String indent) {
    StringBuffer sb = new StringBuffer();
    // indent bug - leave out
    //sb.append( indent );
    sb.append("<");
    sb.append(node.getNodeName());//w  w w  . ja v  a 2  s.  c o m

    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        sb.append(" ");
        Node attrNode = attrs.item(i);
        sb.append(attrNode.getNodeName());
        sb.append("=\"");
        sb.append(attrNode.getNodeValue());
        sb.append("\"");
    }

    if (!node.hasChildNodes()) {
        sb.append("/>");
    } else {
        sb.append(">");
    }

    return sb.toString();
}

From source file:Main.java

public static String domNode2String(Node node, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += domNode2String(nodes.item(i), escapeStrings);
            }/*from  w  w w  .  j a v  a2s.c o  m*/
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                ret += domNode2String(children.item(i), escapeStrings);
            }
        }

        ret += "</" + name + ">";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;
    case Node.COMMENT_NODE:
        ret += "<!--" + node.getNodeValue() + "-->";
        break;
    }
    return ret;
}

From source file:Main.java

/**
 * This method returns node value for given child. If there is no text
 * available for given node, then this method returns null
 * /*ww  w.  ja va 2  s  .com*/
 * @return Node value of input node
 * @throws IllegalArgumentException
 *             if input is invalid
 */
public static String getNodeValue(final Node inputNode) {
    // Child count
    int childCount = 0;

    if (inputNode == null) {
        return null;
    }

    // Return null if child not found
    final NodeList childList = inputNode.getChildNodes();
    if ((childList == null) || (childList.getLength() < 1)) {
        return null;
    }

    // Get child count
    childCount = childList.getLength();

    // For each child
    for (int childIndex = 0; childIndex < childCount; childIndex++) {
        // Get each child
        final Node childNode = childList.item(childIndex);

        // Check if text node
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            // Return node value
            return childNode.getNodeValue();
        }
    }

    // If no text node found return null
    return null;
}

From source file:Main.java

public static String getNodeAttr(String tagName, String attrName, 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.ATTRIBUTE_NODE)
                        && data.getNodeName().equalsIgnoreCase(attrName)) {
                    return data.getNodeValue();
                }//  w  w w. j  ava 2  s  .co  m
            }
        }
    }
    return "";
}