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 String value of the node. /*from   ww  w. j  a  va2  s. co  m*/
 If the node does not contain text then an empty String is returned
 * @param node the node of interest
 * @return the value of that node
 */
public static String getTextForNode(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null) {
        return "";
    }

    for (int i = 0; i < children.getLength(); i++) {
        Node childNode = children.item(i);
        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            return childNode.getNodeValue();
        }
    }

    return "";
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * //w w  w . ja v a2 s. c o m
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:Main.java

public static String getAttributeValue(Node node, String attrName) throws TransformerException {
    String value = null;/*w  ww  .  ja  va 2s  .co  m*/
    node = node.getAttributes().getNamedItem(attrName);
    if (node != null) {
        value = node.getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Collapses a list of CDATASection, Text, and predefined EntityReference
 * nodes into a single string.  If the list contains other types of nodes,
 * those other nodes are ignored.//from   w  w  w. j  a va 2  s . c om
 */
public static String getText(NodeList nodeList) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        switch (node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            buffer.append(node.getNodeValue());
            break;
        case Node.ENTITY_REFERENCE_NODE:
            if (node.getNodeName().equals("amp"))
                buffer.append('&');
            else if (node.getNodeName().equals("lt"))
                buffer.append('<');
            else if (node.getNodeName().equals("gt"))
                buffer.append('>');
            else if (node.getNodeName().equals("apos"))
                buffer.append('\'');
            else if (node.getNodeName().equals("quot"))
                buffer.append('"');
            // Any other entity references are ignored
            break;
        default:
            // All other nodes are ignored
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static String getNamespaceURI(final org.w3c.dom.Node n, final String prefix) {
    Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
    if (prefixDeclaration != null) {
        // we have found the good NameSpace
        return prefixDeclaration.getNodeValue();
    }//from   ww  w  .j a  v  a  2 s  . c  om
    // we have found the good NameSpace
    // we look for the NameSpace in the parent Node
    return getNamespaceURI(n.getParentNode(), prefix);
}

From source file:Main.java

/**
 * Get the text content of an element.// w  w  w. j  a  v a 2s  .  c om
 *
 * @param element
 *            The element.
 * @param sbuf
 *            The buffer to append to.
 * @param decend
 *            Whether to descend into child elements.
 */
public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
    Node node = element.getFirstChild();

    while (node != null) {
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
            sbuf.append(node.getNodeValue());
            break;

        case Node.ELEMENT_NODE:
            if (decend) {
                getText((Element) node, sbuf, decend);
            }

            break;
        }

        node = node.getNextSibling();
    }
}

From source file:Main.java

public static String getText(Node node)

{

    StringBuffer result = new StringBuffer();

    if (!node.hasChildNodes())

        return "";

    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++)

    {/*from  w  w w. ja  v a  2s  . c  o  m*/

        Node subnode = list.item(i);

        if (subnode.getNodeType() == Node.TEXT_NODE)

        {

            result.append(subnode.getNodeValue());

        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE)

        {

            result.append(subnode.getNodeValue());

        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE)

        {

            // Recurse into the subtree for text

            // (and ignore comments)

            result.append(getText(subnode));

        }

    }

    return result.toString();

}

From source file:Main.java

/**
 * Returns the child text of this element, or the default value if there is none.
 * @param element/*from w  w w  . j a  v a2 s  .  co m*/
 * @param defaultValue
 * @return Either the child text of the element or the default value if there is not child text.
 */
public static String getChildText(Element element, String defaultValue) {
    Node childNode = element.getFirstChild();
    if (childNode == null || !(childNode instanceof Text))
        return defaultValue;

    return childNode.getNodeValue();
}

From source file:in.raster.oviyam.util.ReadXMLFile.java

private static String getTagValue(String sTag, Element element) {
    NodeList nodeList = element.getElementsByTagName(sTag).item(0).getChildNodes();
    Node node = (Node) nodeList.item(0);

    if (node != null) {
        return node.getNodeValue();
    } else {/*from  ww w .  j a v  a  2 s. c o m*/
        return "";
    }
}

From source file:Main.java

/**
 * Searches below the supplied Node for a "list" tag and then retrieves the contents of the "value" tag(s) under that.
 * Note that if there is more than one "list" under the node then we will take the values from the first one only.
 * /*from w  w w  .j a  va2  s.  co m*/
 * @param node
 *          acts as the root for the seach
 * 
 * @return list of Strings corresponding to the contents of the "value" tag(s)
 */
public static ArrayList getElementListValues(Node node) {
    ArrayList values = new ArrayList();

    // search for list tag
    Document doc = node.getOwnerDocument();
    NodeList list = doc.getElementsByTagName("list");

    if (list.getLength() == 0)
        return values;

    // search under that for value tag(s)
    doc = list.item(0).getOwnerDocument();
    NodeList vals = doc.getElementsByTagName("value");

    // for each one we get the text contents
    for (int i = 0; i < vals.getLength(); i++) {
        Node v = vals.item(i);
        NodeList text = v.getChildNodes();

        if (text == null) {
            values.add("");
            continue;
        }

        // there should be only text inside the value tag
        Node value = text.item(0);
        if (value == null)
            values.add("");
        else
            values.add(value.getNodeValue());
    }

    return values;
}