Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    public static List<String> getChildrenElementText(Node node, String childTag) {
        ArrayList<String> stringList = new ArrayList<String>();
        if (node == null)
            return stringList;
        List<Node> list = getChildNodes(node, childTag);
        for (Node n : list) {
            if (n != null)
                stringList.add(n.getTextContent().trim());
        }
        return stringList;
    }

    public static List<Node> getChildNodes(Node node, String childTag) {
        ArrayList<Node> nodes = new ArrayList<Node>();
        if (node == null)
            return nodes;
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node childNode = nodeList.item(i);
            if (childNode != null && childNode.getLocalName() != null
                    && childNode.getLocalName().equals(childTag)) {
                nodes.add(childNode);
            }
        }
        return nodes;
    }
}