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

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another.//from  ww w.  j a  v  a 2s.c  o  m
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}

From source file:Main.java

public static String getTextContent(Node e) {
    StringBuilder sb = new StringBuilder();

    NodeList children = e.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.TEXT_NODE) {
                sb.append(n.getNodeValue());
            }//  w  w w .j a  va2 s. c o m
        }
    }

    return sb.toString();
}

From source file:Main.java

public static Properties getElementAttributes(Element ele) {
    Properties ht = new Properties();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);//from  w w  w  . jav a  2s  .c om
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}

From source file:Main.java

/**
 * /*from   w w  w  .  j  a  va 2 s.  c o m*/
 * @param node
 * @param name
 * @return
 */
public static String getStringAttribute(Node node, String name) {
    Node attribute = node.getAttributes().getNamedItem(name);
    if (attribute != null) {
        return attribute.getNodeValue();
    } else {
        return null;
    }
}

From source file:Main.java

public static String getOnlyIdOfXmiAttribute(NodeList elements, int i) {
    Node href = elements.item(i).getAttributes().getNamedItem("href");
    if (href != null) {
        String currentValue = href.getNodeValue();
        return currentValue.substring(currentValue.indexOf("#") + 1, currentValue.length());
    } else {// w w  w  .  j  a v  a  2s. c  o  m
        return null;
    }
}

From source file:Main.java

/**
 * Gets <code>CDATASection</code> element for node.
 *
 * @param node node with text.//from ww w.  j a  v  a2 s.  c  o m
 * @return text, that contains the specified node.
 */
public static String getNodeCDATASection(final Node node) {
    //        node.normalize();
    final NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        final Node child = list.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE)
            return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

static public String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE) {
            return data.getNodeValue();
        }/*from w w  w  .  j a  v  a  2  s .c o m*/
    }
    return "";
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    NamedNodeMap m = node.getAttributes();
    if (m == null)
        return null;
    Node att = m.getNamedItem(name);
    if (att == null)
        return null;
    return att.getNodeValue();
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);/*www.j  a va 2 s  .  c om*/
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}

From source file:Main.java

public static void setIdentityAttribute(Document document, String attributeValue) {
    boolean isFound = false;
    NodeList propertyList = document.getElementsByTagName("Property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        Node node = propertyList.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals("name") && attribute.getNodeValue().equals(attributeValue)) {
                Element element = (Element) node;
                element.setAttribute("isIdentity", "true");
                isFound = true;/*from   w w  w . ja va  2s  . com*/
                break;
            }
        }
        if (isFound)
            break;
    }
}