Checks if the Element contains all of the attributes contained in the list of labels, or throws an XMLParserException if it does not. - Java XML

Java examples for XML:XML Attribute

Description

Checks if the Element contains all of the attributes contained in the list of labels, or throws an XMLParserException if it does not.

Demo Code


import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Comment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main{
    /**/*from   w w w  . j av  a  2  s.c o m*/
     * Checks if the Element contains all of the attributes contained in the
     * list of labels, or throws an {@link XMLParserException} if it does not.
     *
     * @param element
     *            The Element object.
     * @param labels
     *            The list of string labels to test.
     * @throws XMLParserException
     *             If the Element object does not contain an element required in
     *             the list of labels, an {@link XMLParserException} will be
     *             thrown.
     */
    public static void checkedAllAttributes(Element element, String[] labels)
            throws XMLParserException {
        for (String label : labels)
            if (!element.hasAttribute(label))
                throw new XMLParserException(String.format(
                        "Tag %s missing attribute %s.",
                        element.getNodeName(), label));
        return;
    }
}

Related Tutorials