Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * 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();
        }
    }
}