Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedList;

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

public class Main {

    public static String[] getChildrenText(Element parentElement, String childrenName) {
        NodeList nl = parentElement.getChildNodes();
        int max = nl.getLength();
        LinkedList<String> list = new LinkedList<String>();
        for (int i = 0; i < max; i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
                list.add(getText((Element) n));
            }
        }
        return list.toArray(new String[list.size()]);
    }

    /**
     * 
     * @param e
     * @return
     */
    public static String getText(Element e) {
        NodeList nl = e.getChildNodes();
        int max = nl.getLength();
        for (int i = 0; i < max; i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.TEXT_NODE) {
                return n.getNodeValue().trim();
            }
        }
        return "";
    }
}