Java XML Child Element Attribute getChildAttribute(Element root, String childName, String attName)

Here you can find the source of getChildAttribute(Element root, String childName, String attName)

Description

Returns a particular attribute value of a specified child of an element.

License

Open Source License

Parameter

Parameter Description
root The element.
childName The child element.
attName The attribute of the child to obtain.

Return

The attribute value or null if it does not exist.

Declaration

public static String getChildAttribute(Element root, String childName,
        String attName) 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans/*from   w  w w.j  av  a2 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;
import org.w3c.dom.Node;

public class Main {
    /**
     * Returns a particular attribute value of a specified child of an element.
     * 
     * @param root The element.
     * @param childName The child element.
     * @param attName The attribute of the child to obtain.
     * 
     * @return The attribute value or null if it does not exist.
     * 
     */
    public static String getChildAttribute(Element root, String childName,
            String attName) {
        try {
            return getChildAttribute(root, childName, attName, false);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns a particular attribute value of a specified child of an element.
     * 
     * @param root The element.
     * @param childName The child element.
     * @param attName The attribute of the child to obtain.
     * @param mandatory If its mandatory that the attribute exist.
     * 
     * @return The attribute value
     * 
     * @throws Exception If mandatory is set it to <code>true</code> and the child 
     * or attribute do not exist.
     */
    public static String getChildAttribute(Element root, String childName,
            String attName, boolean mandatory) throws Exception {
        Element elem = getChildElement(root, childName);
        if (elem == null) {
            if (mandatory) {
                throw new Exception("No such child: " + childName);
            }

            return null;
        }

        if (mandatory && !elem.hasAttribute(attName)) {
            throw new Exception("No such attribute: " + attName);

        }
        return elem.getAttribute(attName);
    }

    /**
     * getChildElement purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child element of
     * the specified name.  An exception occurs when the node is required and
     * not found.
     * </p>
     *
     * @param root The root element to look for children in.
     * @param name The name of the child element to look for.
     * @param mandatory true when an exception should be thrown if the child
     *        element does not exist.
     *
     * @return The child element found, null if not found.
     *
     * @throws Exception When a child element is required and not
     *         found.
     */
    public static Element getChildElement(Element root, String name,
            boolean mandatory) throws Exception {
        Node child = root.getFirstChild();

        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (name.equals(child.getNodeName())) {
                    return (Element) child;
                }
            }

            child = child.getNextSibling();
        }

        if (mandatory && (child == null)) {
            throw new Exception(root.getNodeName()
                    + " does not contains a child element named " + name);
        }

        return null;
    }

    /**
     * getChildElement purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child element of
     * the specified name.
     * </p>
     *
     * @param root The root element to look for children in.
     * @param name The name of the child element to look for.
     *
     * @return The child element found, null if not found.
     *
     * @see #getChildElement(Element,String,boolean)
     */
    public static Element getChildElement(Element root, String name) {
        try {
            return getChildElement(root, name, false);
        } catch (Exception e) {
            //will never be here.
            return null;
        }
    }

    /**
     * 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. getChildAttribute(Node n, String childName, String attribute)
  2. getChildAttributNode(Node node)