Java XML NodeList extractNodeListFromElement(Element config, String tag, boolean required)

Here you can find the source of extractNodeListFromElement(Element config, String tag, boolean required)

Description

extract Node List From Element

License

Open Source License

Declaration

public static NodeList extractNodeListFromElement(Element config, String tag, boolean required) 

Method Source Code

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

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static NodeList extractNodeListFromElement(Element config, String tag, boolean required) {
        NodeList node = config.getElementsByTagName(tag);
        return check1Most(node, tag, required);
    }/*from   ww w.j a v  a 2s  .  com*/

    public static NodeList check1Most(NodeList node, String tag, boolean required) {
        if (node.getLength() == 0) {
            if (!required) {
                return null;
            }
            throw new RuntimeException("Tag " + tag + " is required in configuration file");
        } else if (node.getLength() > 1) {
            throw new RuntimeException("Only one tag " + tag + " is required in configuration file");
        }
        return node.item(0).getChildNodes();
    }
}

Related

  1. copyNodeList(NodeList nodeList)
  2. createNodeCollection(final NodeList nodeList)
  3. createRealNodeList(NodeList nodeList)
  4. equalNodes(NodeList sourceNodeList, NodeList targetNodeList)
  5. extractElementsFromNodeList(NodeList config, String tag, boolean required)
  6. fillHashtable(NodeList list, Hashtable fillIn)
  7. fillHashtable(NodeList list, Hashtable fillIn)
  8. findElement(NodeList elements, String elementName)
  9. findElementsIgnoreCase(String name, NodeList nodes)