Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Get a list of all child text Elements of given name directly
     * under a given {@code org.w3c.dom.Element}.
     *
     * @param elem the parent Element
     * @param name the given name of searched child Elements
     * @return a List of values of those child text Elements
     */
    public static List<String> getAllElementsByTagName(Element elem, String name) {
        NodeList nodeList = elem.getElementsByTagName(name);
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < nodeList.getLength(); ++i) {
            NodeList children = nodeList.item(i).getChildNodes();
            if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) {
                continue;
            }
            result.add(children.item(0).getNodeValue());
        }
        return result;
    }
}