Java XML Node Name getNodeContent(Node item, String nodeName)

Here you can find the source of getNodeContent(Node item, String nodeName)

Description

Gets the content of a subnode.

License

Open Source License

Declaration

public static Optional<String> getNodeContent(Node item, String nodeName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Optional;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  w ww  . ja va 2s .c  om*/
     * Gets the content of a subnode.
     * For example,
     * <item>
     *     <nodeName>content</nodeName>
     * </item>
     */
    public static Optional<String> getNodeContent(Node item, String nodeName) {
        if (item.getNodeType() != Node.ELEMENT_NODE) {
            return Optional.empty();
        }

        NodeList metadata = ((Element) item).getElementsByTagName(nodeName);
        if (metadata.getLength() == 1) {
            return Optional.ofNullable(metadata.item(0).getTextContent());
        } else {
            return Optional.empty();
        }
    }
}

Related

  1. getNode(final Node iNode, final String iNodeName)
  2. getNode(Node iNode, String iNodeName)
  3. getNode(Node iNode, String iNodeName)
  4. getNode(Node node, String nodeName)
  5. getNode(Node node, String nodeName)
  6. getNodeContent(Node n, String nodename)
  7. getNodeName(Node node)
  8. getNodeName(Node node)
  9. getNodeName(Node node)