Java examples for XML:XML Attribute
Checks if the Element contains any of the attributes contained in the list of labels, or throws an XMLParserException if it does not.
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 www . j av a2 s .c o m*/ * Checks if the Element contains any 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 at least one of the * elements tested in the list of labels, an * {@link XMLParserException} will be thrown. */ public static void checkedAnyAttributes(Element element, String[] labels) throws XMLParserException { for (String label : labels) if (element.hasAttribute(label)) return; throw new XMLParserException(String.format( "Tag %s missing at least one of any attribute: %s.", element.getNodeName(), listToString(labels))); } /** * Converts the list of labels to a string. * * @param labels * The list of labels. * @return The string version of the list. */ private static String listToString(String[] labels) { StringBuilder s = new StringBuilder(); for (String j : labels) s.append(j).append(", "); String s1 = s.toString(); return s1.substring(0, s1.length() - 2); } /** * Converts the list of labels to a string. * * @param labels * The list of labels. * @return The string version of the list. */ private static String listToString(List<String> labels) { return listToString(labels.toArray(new String[0])); } }