Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import org.w3c.dom.Element;

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

public class Main {

    public static ArrayList getChildTextNodeValues(NodeList theNodeList) {
        ArrayList arrayList = new ArrayList();

        for (int i = 0; i < theNodeList.getLength(); i++) {
            Node node = getTextNode(theNodeList.item(i));
            if (node != null) {
                String s = node.getNodeValue();
                arrayList.add(s);
            }
        }
        return arrayList;
    }

    public static ArrayList getChildTextNodeValues(Node theNode) {
        ArrayList arrayList = new ArrayList();
        if (theNode.getNodeType() != Node.TEXT_NODE) {
            Element theNodeElement = (Element) theNode;
            NodeList theNodeList = theNodeElement.getChildNodes();
            for (int i = 0; i < theNodeList.getLength(); i++) {
                Node node = getTextNode(theNodeList.item(0));
                if (node != null) {
                    String s = node.getNodeValue();
                    arrayList.add(s);
                }
            }
        }
        return arrayList;
    }

    public static Node getTextNode(Node node) {
        Node textnode = null;
        NodeList nodes = node.getChildNodes();
        int len = nodes.getLength();
        if (len > 0) {
            for (int i = 0; i < len; i++) {
                if (nodes.item(i).getNodeType() == Node.TEXT_NODE) {
                    textnode = nodes.item(i);
                    break;
                }
            }
        }
        return textnode;
    }

    public String getNodeType(Node node) {
        String type;

        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE: {
            type = "Element";
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            type = "Attribute";
            break;
        }
        case Node.TEXT_NODE: {
            type = "Text";
            break;
        }
        case Node.CDATA_SECTION_NODE: {
            type = "CData section";
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            type = "Entity reference";
            break;
        }
        case Node.ENTITY_NODE: {
            type = "Entity";
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            type = "Processing instruction";
            break;
        }
        case Node.COMMENT_NODE: {
            type = "Comment";
            break;
        }
        case Node.DOCUMENT_NODE: {
            type = "Document";
            break;
        }
        case Node.DOCUMENT_TYPE_NODE: {
            type = "Document type";
            break;
        }
        case Node.DOCUMENT_FRAGMENT_NODE: {
            type = "Document fragment";
            break;
        }
        case Node.NOTATION_NODE: {
            type = "Notation";
            break;
        }
        default: {
            type = "???";
            break;
        }
        }
        return type;
    }
}