Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

import java.util.*;

public class Main {
    /**
     * Find all child node(immediate children only) under current node that is an element node and node name is tagName
     * This is  a combination of getChildNodes() and getElementByTagName().
     * Also this helper method returns an iterable node list for convinient use in the foreach statement.
     * Only element node directly under currentNode are returned.(i.e no grandchildren).   
     * The order of the children are maintained (removing non-element node) 
     * @param currentNode
     * @param tagName - case sensitive
     * @return list of element nodes equals tagname (case sensitive) 
     */
    public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
        List<Node> results = new ArrayList<Node>();
        NodeList childNodes = currentNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
                results.add(child);
        }
        return results;
    }
}