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

/**
 * Getter for the attribute value of an attribute of the given name
 * for the specified node. The attribute value is returned as a string.
 * @param node Node./*from  w  ww.  j  a  v a  2  s.c o m*/
 * @param name Name of the attribute.
 * @return Returns the attribute value as string or null if the attribute
 * does not exist.
 */
static public String getAttrValString(Node node, String name) {
    NamedNodeMap attr = node.getAttributes();
    if (attr != null) {
        node = attr.getNamedItem(name);
        if (node != null)
            return (node.getNodeValue());
    }
    return (null);
}

From source file:Main.java

public static String getAttrValue(NamedNodeMap attrs, String attrName) {
    if (attrs == null || attrName == null) {
        return null;
    }/*from   w ww .  j av  a 2s.c o m*/
    String attrValue = null;

    Node attrNode = attrs.getNamedItem(attrName);
    if (attrNode == null || Node.ATTRIBUTE_NODE != attrNode.getNodeType()) {
        return null;
    }

    attrValue = attrNode.getNodeValue();
    return attrValue;
}

From source file:Main.java

public static String getElementBody(Element element) {
    org.w3c.dom.Node valueNode = element.getFirstChild();
    if (valueNode == null) {
        return null;
    }/* www .j  a  v a  2s  .com*/
    if (valueNode.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
        return valueNode.getNodeValue();
    } else {
        return null;
    }
}

From source file:Main.java

public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // get child node
        Node childNode = list.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                    + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

            String nodeValue = childNode.getNodeValue();
            nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
            if (!nodeValue.isEmpty()) {
                System.out.println(nodeValue);
                bw.write(nodeValue);/*  w  w  w .j av  a 2  s  . c  o m*/
                bw.newLine();
            }
        }
        visitRecursively(childNode, bw);
    }
}

From source file:Main.java

/**
 * Returns null if the attribute doesn't exist, otherwise returns the attribute.
 * @param node// w w w  . j av a 2  s.  c  o m
 * @param attribute
 * @return
 */
public static String getAttribute(Node node, String attribute) {
    Node attributeNode = node.getAttributes().getNamedItem(attribute);

    if (attributeNode == null) {
        return null;
    }
    return node.getNodeValue();
}

From source file:Main.java

public static String getText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);

        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            sb.append(child.getNodeValue());
        }//w  ww  .j  a v  a  2 s  . c  om
    }

    return sb.toString();
}

From source file:Main.java

public static String getNodeValue(final Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Node n = node.getFirstChild();
        if (n != null) {
            do {/*from  w  w w .  j a  v  a2s.c  om*/
                if (n.getNodeType() == Node.TEXT_NODE) {
                    return n.getNodeValue();
                }
            } while ((n = n.getNextSibling()) != null);
        }
    }

    return node.getNodeValue();
}

From source file:Main.java

/**
 * Obtains the text of the specified element.
 *
 * @param element the text element./* ww w. j a  v a 2s  .  com*/
 * @return the text value inside the element.
 */
public static String getText(final Element element) {
    final Node node = element.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE) {
        return node.getNodeValue().trim();
    }
    return null;
}

From source file:Main.java

/**
 * This is an ugly hack, there must be a nicer way to do it.
 * Using getPrefix doesn't work because it stops us from getting
 * xmlns: attributes, which is what I'm using this for. Using
 * getNamespace doesn't seem to work on xmlns attributes either.
 * Trims the prefix on the map key./*from   ww w.  j a  va2 s.  c o m*/
 * @param element the element from which to retrieve the attributes.
 * @param prefix the prefix of the attributes to retrieve
 * @return a Map containing the attributes names and their values.
 */
public static Map getAttributesWithPrefix(Element element, String prefix) {
    Map result = new HashMap();

    prefix += ":";

    NamedNodeMap attributes = element.getAttributes();

    if (attributes == null)
        return result;

    for (int i = 0; i != attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (attribute.getNodeName().startsWith(prefix)) {
            result.put(attribute.getNodeName().substring(prefix.length()), attribute.getNodeValue());
        }
    }

    return result;
}

From source file:Main.java

public static ArrayList getChildTextNodeValues(Node theNode) {
    ArrayList arrayList = new ArrayList();
    if (theNode.getNodeType() != Node.TEXT_NODE) {
        Element theNodeElement = (Element) theNode;
        NodeList theNodeList = theNodeElement.getChildNodes();
        for (int i = 0; i < theNodeList.getLength(); i++) {
            Node node = getTextNode(theNodeList.item(0));
            if (node != null) {
                String s = node.getNodeValue();
                arrayList.add(s);//from ww  w. j a v a2  s  . com
            }
        }
    }
    return arrayList;
}