Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**
     * get the first element in the parent element that's named name or null if no such element exists
     */
    public static Element getFirstElement(Element parent, String name) {
        NodeList list = parent.getElementsByTagName(name);

        Node node = list.item(0);
        if (node == null) {
            //throw new RuntimeException("Malformed XML: No such node: " + name);
            return null;
        }

        if (!isElement(node)) {
            throw new RuntimeException("XML Parsing error: " + name + " is not an Element");
        }

        return (Element) node;
    }

    public static boolean isElement(Node n) {
        return (n.getNodeType() == Node.ELEMENT_NODE);
    }
}