Java XML Attribute Get getAttribute(Element elem, String attName, boolean mandatory)

Here you can find the source of getAttribute(Element elem, String attName, boolean mandatory)

Description

getIntAttribute purpose.

License

Open Source License

Parameter

Parameter Description
elem The root element to look for children in.
attName The name of the attribute to look for.
mandatory true when an exception should be thrown if the attribute element does not exist.

Exception

Parameter Description
Exception When a child attribute is required andnot found.
NullPointerException DOCUMENT ME!

Return

The value if the attribute was found, the null otherwise.

Declaration

public static String getAttribute(Element elem, String attName,
        boolean mandatory) throws Exception 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans//w w w .ja  v  a 2 s . c o  m
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    /**
     * getIntAttribute purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child integer
     * attribute of the specified name.  An exception occurs when the node is
     * required and not found.
     * </p>
     *
     * @param elem The root element to look for children in.
     * @param attName The name of the attribute to look for.
     * @param mandatory true when an exception should be thrown if the
     *        attribute element does not exist.
     *
     * @return The value if the attribute was found, the null otherwise.
     *
     * @throws Exception When a child attribute is required and
     *         not found.
     * @throws NullPointerException DOCUMENT ME!
     */
    public static String getAttribute(Element elem, String attName,
            boolean mandatory) throws Exception {
        if (elem == null) {
            if (mandatory) {
                throw new NullPointerException();
            }

            return "";
        }

        Attr att = elem.getAttributeNode(attName);

        String value = null;

        if (att != null) {
            value = att.getValue();
        }

        if (mandatory) {
            if (att == null) {
                throw new Exception("element " + elem.getNodeName()
                        + " does not contains an attribute named "
                        + attName);
            } else if ("".equals(value)) {
                throw new Exception("attribute " + attName + "in element "
                        + elem.getNodeName() + " is empty");
            }
        }

        return value;
    }
}

Related

  1. getAttribute(Element e, String name)
  2. getAttribute(Element e, String name)
  3. getAttribute(Element el, String attrName)
  4. getAttribute(Element el, String attrName)
  5. getAttribute(Element el, String attrName)
  6. getAttribute(Element elem, String attName, boolean mandatory)
  7. getAttribute(Element elem, String name, String def)
  8. getAttribute(Element element, String attr)
  9. getAttribute(Element element, String attribute)