Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Iterator;
import java.util.NoSuchElementException;

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

public class Main {
    /**
     * Extracts child elements from node whith type <code>ELEMENT_NODE</code>.
     *
     * @param node root node of XML document for search.
     * @return iterator with proper node childs.
     */
    public static Iterator<Element> getChildElements(final Node node) {
        //        node.normalize();
        return new Iterator<Element>() {
            private final NodeList nodes = node.getChildNodes();
            private int nextPos = 0;
            private Element nextElement = seekNext();

            public boolean hasNext() {
                return nextElement != null;
            }

            public Element next() {
                if (nextElement == null)
                    throw new NoSuchElementException();
                final Element result = nextElement;
                nextElement = seekNext();
                return result;
            }

            public void remove() {
                throw new UnsupportedOperationException("operation not supported");
            }

            private Element seekNext() {
                for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                    final Node childNode = nodes.item(i);
                    if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                        nextPos = i + 1;
                        return (Element) childNode;
                    }
                }
                return null;
            }
        };
    }

    /**
     * Extracts child elements with given name from node whith type <code>ELEMENT_NODE</code>.
     *
     * @param node        root node of XML document for search.
     * @param elementName name of elements that should be returned.
     * @return iterator with proper node childs.
     */
    public static Iterator<Element> getChildElements(final Node node, final String elementName) {
        //        node.normalize();
        return new Iterator<Element>() {
            private final NodeList nodes = node.getChildNodes();
            private int nextPos = 0;
            private Element nextElement = seekNext();

            public boolean hasNext() {
                return nextElement != null;
            }

            public Element next() {
                if (nextElement == null)
                    throw new NoSuchElementException();
                final Element result = nextElement;
                nextElement = seekNext();
                return result;
            }

            public void remove() {
                throw new UnsupportedOperationException("operation not supported");
            }

            private Element seekNext() {
                for (int i = nextPos, len = nodes.getLength(); i < len; i++) {
                    final Node childNode = nodes.item(i);
                    if (childNode.getNodeType() == Node.ELEMENT_NODE
                            && childNode.getNodeName().equals(elementName)) {
                        nextPos = i + 1;
                        return (Element) childNode;
                    }
                }
                return null;
            }
        };
    }
}