Java XML Element Get from Parent getElements(Document document, Element parent)

Here you can find the source of getElements(Document document, Element parent)

Description

get Elements

License

Apache License

Declaration

public static Element[] getElements(Document document, Element parent) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

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

public class Main {
    private static Element[] EMPTY_ELEMENTS = new Element[0];

    public static Element[] getElements(Document document, Element parent) {
        if (parent == null) {
            return new Element[] {};
        }// w w w . j a  va  2 s.co m

        NodeList nl = parent.getChildNodes();
        return toElements(nl);
    }

    private static Element[] toElements(NodeList nl) {
        if (nl.getLength() == 0) {
            return EMPTY_ELEMENTS;
        }
        List<Element> al = new ArrayList<Element>();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n instanceof Element) {
                al.add((Element) n);
            }
        }
        return al.toArray(new Element[al.size()]);
    }
}

Related

  1. getElementIntValue(Document document, Element parent, String string)
  2. getElements(Document document, Element parent)
  3. getElementStringValue(Document document, Element parent, String element)
  4. getElementValueByTagName(Document doc, String parentName, String eleName)
  5. getFirstElement(Node parent)
  6. getFirstElement(Node parent)