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

/**
 * Returns the value of the given attribute.
 *
 * @param node     The current node//from  www  .  ja  va 2s.c  o m
 * @param attrName The attribute name
 * @return The attribute's value
 */
public static String getAttribute(Node node, String attrName) {
    // handle null node
    if (node == null) {
        return null;
    }
    Node attr = node.getAttributes().getNamedItem(attrName);
    if (attr != null) {
        return attr.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 * We get a object that connected text node of the child node and the CDATA section as character string.
 * @param node//from w w w . ja  v  a 2s  . c  o  m
 * @return
 */
public static String getChildText(Node node) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            buf.append(n.getNodeValue());
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Searches for a particular attribute attached to a node. Returns null
 * if not found//w w w . ja v a 2  s. co m
 */
public static String searchForAttribute(Node content, String attributeName) {
    if (content == null) {
        return null;
    } else if (attributeName == null) {
        return null;
    }

    Node node = content.getAttributes().getNamedItem(attributeName);

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

From source file:Main.java

public static String getAttribute(final Node node, final String attributeName) {
    final NamedNodeMap tmpMap = node.getAttributes();
    if (tmpMap == null) {
        return null;
    }/*from  w w  w . ja  v a 2 s . c o m*/
    final Node tmpNode = tmpMap.getNamedItem(attributeName);
    if (tmpNode == null) {
        return null;
    }
    return tmpNode.getNodeValue();
}

From source file:Main.java

/**
 * @param node//from ww w  .j a v a2 s. c o m
 * @param attributeName
 * @return
 */
public static String getNodeAttribute(Node node, String attributeName) {
    String sRetValue = null;
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        //--- Find the attribute 
        Node attrib = attributes.getNamedItem(attributeName);
        if (attrib != null) {
            //--- Get the attribute value
            sRetValue = attrib.getNodeValue();
        }
    }
    return sRetValue;
}

From source file:ApplyXPathJAXP.java

static void printNode(Node node) throws Exception {
    if (isTextNode(node)) {
        System.out.println(node.getNodeValue());
    } else {//from  ww w  .  j  a  va2  s  .  c  o  m
        // Set up an identity transformer to use as serializer.
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(System.out)));
    }

}

From source file:Main.java

public static String getTextValue(Element valueEle) {

    StringBuffer value = new StringBuffer();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }//from www.  ja va2s  .  c  o m
    }
    return value.toString().trim();
}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys://from w  w  w .  ja v  a 2  s.  co m
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

    NodeList childNodes = sourceNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node element = childNodes.item(i);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

public static ArrayList<Node> getNodesWithKey(Node parent, String key, Set<String> values,
        boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    Node current;/*  ww  w  .  ja v a2s.co  m*/
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

public static String getTextNodeValueByChildTagName(Element ele, String tagname) {
    String s = "";
    try {//from   ww w.  j  a v  a2 s.  c o m
        NodeList nodes = ele.getElementsByTagName(tagname);
        if (nodes.getLength() > 0) {
            Node node = getTextNode(nodes.item(0));
            if (node != null) {
                s = node.getNodeValue();
            }
        }
    } catch (Exception e) {
        s = "";
    }

    return s;
}