Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    public static String getNextSiblingElementText(Node node) {
        return getElementText(getNextSiblingElement(node));
    }

    public static String getNextSiblingElementText(Node node, String elemName) {
        return getElementText(getNextSiblingElement(node, elemName));
    }

    public static String getElementText(Element ele) {

        // is there anything to do?
        if (ele == null) {
            return null;
        }
        // get children text
        Node child = ele.getFirstChild();
        if (child != null) {
            short type = child.getNodeType();
            if (type == Node.TEXT_NODE) {
                return child.getNodeValue();
            }
        }
        // return text value
        return null;
    }

    /** Finds and returns the next sibling element node. */
    public static Element getNextSiblingElement(Node node) {

        if (node == null)
            return null;
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) sibling;
            }
            sibling = sibling.getNextSibling();
        }

        // not found
        return null;

    }

    /** Finds and returns the next sibling node with the given name. */
    public static Element getNextSiblingElement(Node node, String elemName) {

        if (node == null)
            return null;
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                if (sibling.getNodeName().equals(elemName)) {
                    return (Element) sibling;
                }
            }
            sibling = sibling.getNextSibling();
        }

        // not found
        return null;

    }
}