Utils.java Source code

Java tutorial

Introduction

Here is the source code for Utils.java

Source

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Utils {
    public static String getFirstText(Node parent) {
        return getTextNodeByNumber(parent, 1);
    }

    public static String getTextNodeByNumber(Node parent, int number) {
        String text = null;
        int count = 1;

        if (parent != null) {
            for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {

                if ((child.getNodeType() == Node.TEXT_NODE) && (count++ == number)) {
                    text = child.getNodeValue();
                    return text.trim();
                }
            }
        }
        return text;
    }
}