Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * XMLUtility.java  2/6/13 1:04 PM
 *
 * Copyright (C) 2012-2013 Nick Ma
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import java.util.Enumeration;

import java.util.Vector;

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

public class Main {
    /**
     * Searches parent node for matching child nodes and collects and returns
     * their values.
     *
     * @param node     The parent node
     * @param elemName The matching child node element name
     * @return List of node values
     */
    public static Enumeration getChildrenNodeValues(Node node, String elemName) {
        Vector vect = new Vector();
        NodeList nl = node.getChildNodes();
        int n = nl.getLength();
        for (int i = 0; i < n; i++) {
            Node nd = nl.item(i);
            if (nd.getNodeName().equals(elemName)) {
                Node child = nd.getFirstChild();
                if (child == null) {
                    vect.add("");
                } else {
                    vect.add(child.getNodeValue());
                }
            }
        }
        return vect.elements();
    }

    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     * @param node     The parent node
     * @return The child node's value
     */
    public static String getNodeValue(Node node) {
        // sometimes jdom use more than one children nodes to represent text node 
        NodeList children = node.getChildNodes();
        String value = "";
        for (int i = 0; i < children.getLength(); i++) {
            value += children.item(i).getNodeValue();
        }
        return value;
    }

    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     *
     * @param node     The parent node
     * @param nodeName The child node element name
     * @return The child node's value
     */
    public static String getNodeValue(Node node, String nodeName) {
        Node n = getNode(node, nodeName);
        if (n == null) {
            return null;
        }
        Node ch = n.getFirstChild();
        if (ch != null) {
            return ch.getNodeValue();
        }
        return null;

    }

    /**
     * Searches parent node and returns first found matching node.
     *
     * @param node     The parent node
     * @param nodeName The child node's element name
     * @return The first matching child Node
     */
    public static Node getNode(Node node, String nodeName) {
        NodeList ch = node.getChildNodes();
        int l = ch.getLength();
        for (int i = 0; i < l; i++) {
            Node n = ch.item(i);
            if (n.getNodeName().equals(nodeName)) {
                return n;
            }
        }
        return null;
    }
}