Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

public class Main {
    public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) {
        NodeList nl = e.getChildNodes();
        Element result = null;
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (result != null) // more than one Element children 
                    throw exc;
                result = (Element) n;
            }
        }

        if (result == null // zero Element children
                || !result.getTagName().equals(tag)) // wrong tag
            throw exc;
        return result;
    }
}