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

/** Locate a sub-element tagged 'name', return its value.
 *
 *  Will only go one level down, not search the whole tree.
 *
 *  @param element Element where to start looking. May be null.
 *  @param name Name of sub-element to locate.
 *  @param default_value Default value if not found
 *
 *  @return Returns string that was found or default_value.
 *//*  w  w w .  j  a  va2s  .c o  m*/
final public static String getSubelementString(final Element element, final String name,
        final String default_value) {
    if (element == null)
        return default_value;
    Node n = element.getFirstChild();
    n = findFirstElementNode(n, name);
    if (n != null) {
        Node text_node = n.getFirstChild();
        if (text_node == null)
            return default_value;
        return text_node.getNodeValue();
    }
    return default_value;
}

From source file:Utils.java

/**
 * // ww w . j  ava2  s.  com
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static String getFirstTextNodeVal(Node node) {
    node = getFirstTextNode(node);/*  w ww .ja va 2  s. co m*/
    if (node != null)
        return node.getNodeValue();

    return null;
}

From source file:Main.java

public static String getBody(Element element) {
    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() == 0)
        return null;

    Node firstNode = nodes.item(0);
    if (firstNode == null)
        return null;

    String nodeValue = firstNode.getNodeValue();
    return replace(nodeValue);
}

From source file:Main.java

/**
 * Transform this {@link Node} into a {@link String} representation.
 *
 * @param xml//from   ww  w. ja  v a  2 s . co m
 *        the xml Node
 * @return a String representation of the given xml Node
 */
public static String toString(Node xml) {
    short type = xml.getNodeType();

    if (type == Node.TEXT_NODE)
        return xml.getNodeValue();

    StringWriter buffer = new StringWriter();
    try {
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(xml), new StreamResult(buffer));
    } catch (IllegalArgumentException | TransformerException e) {
        //throw new XMLHandlingException("Cannot create String from Node '" + xml + "'.");
        return "";
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * Returns {@code true} iff the node has the attribute {@code attributeName} with a value that
 * matches one of {@code attributeValues}.
 */// w  w w .  java  2  s . co m
static boolean nodeMatchesAttributeFilter(final Node node, final String attributeName,
        final List<String> attributeValues) {
    if (attributeName == null || attributeValues == null) {
        return true;
    }

    final NamedNodeMap attrMap = node.getAttributes();
    if (attrMap != null) {
        Node attrNode = attrMap.getNamedItem(attributeName);
        if (attrNode != null && attributeValues.contains(attrNode.getNodeValue())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

public static String getNodeValue(String tagName, 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.TEXT_NODE)
                    return data.getNodeValue();
            }//from  w ww. j ava  2  s.  c  o  m
        }
    }
    return "";
}

From source file:Main.java

/**
 * Returns true if the node has some actual content (other than a comment or an
 * empty text)./* w w  w  .java  2  s .  co m*/
 * 
 * @param node the XML node
 * @return true if the node contains information, false otherwise
 */
public static boolean hasContent(Node node) {
    if (node.getNodeName().equals("#comment")) {
        return false;
    } else if (node.getNodeName().equals("#text")) {
        return !node.getNodeValue().trim().isEmpty();
    }
    return true;
}

From source file:Main.java

/**
 * @param n Node to examine//ww w .  j  a v a  2 s.c o m
 * @param attr Attribute to look for
 * @param def Default value to return if attribute is not present
 * @return if the Node contains the named Attribute, the value, if not, the def parameter
 */
public static String getAttribute(Node n, String attr, String def) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null)
        return def;
    Node ret = attrs.getNamedItem(attr);
    if (ret == null)
        return def;
    else
        return ret.getNodeValue();
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node == null)
        return null;
    StringBuffer buff = new StringBuffer();
    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
        Node cn = node.getChildNodes().item(c);
        if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
            buff.append(cn.getNodeValue());
        }/* ww w.j ava 2 s  . com*/
    }
    return buff.toString().trim();

}