Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Random;

import org.w3c.dom.Document;

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

public class Main {

    public static String getText(Node node) {
        if (node == null)
            return "";
        return node.getTextContent();
    }

    public static String getText(Document doc, String tagName) {
        return getText(doc, tagName, 0);
    }

    public static String getText(Document doc, String tagName, boolean flag) {

        NodeList nodeList = doc.getElementsByTagName(tagName);
        int leng = nodeList.getLength();
        if (leng <= 0)
            return "";

        Random rnd = new Random();
        int ran = rnd.nextInt(leng);

        return nodeList.item(ran).getTextContent();
    }

    private static String getText(Document doc, String tagName, int idx) {
        NodeList nodeList = doc.getElementsByTagName(tagName);
        if (nodeList.getLength() <= 0)
            return "";
        return nodeList.item(idx).getTextContent();
    }
}