Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import org.w3c.dom.Element;

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

public class Main {
    public static String getTextElementValue(String elementName, Element parentElement, boolean showEmptyAsNull) {
        ArrayList<Node> childNodesWithTagName = getChildNodesWithTagName(elementName, parentElement);

        if (childNodesWithTagName.size() > 0) {
            Node childNode = childNodesWithTagName.get(0);
            String text = childNode.getFirstChild().getNodeValue().trim();

            return text.equals("") && showEmptyAsNull ? null : text;
        }

        return null;
    }

    public static ArrayList<Node> getChildNodesWithTagName(String tagName, Element parentElement) {
        NodeList childNodes = parentElement.getChildNodes();

        ArrayList<Node> childNodesWithTagName = new ArrayList<Node>();

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node childNode = childNodes.item(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals(tagName)) {
                childNodesWithTagName.add(childNode);
            }
        }

        return childNodesWithTagName;
    }
}