Java XML Child Element Text getChildText(Element root, String childName)

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

Description

getChildText purpose.

License

Open Source License

Parameter

Parameter Description
root The root element to look for children in.
childName The name of the attribute to look for.

Return

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

Declaration

public static String getChildText(Element root, String childName) 

Method Source Code

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

import java.util.logging.Level;
import java.util.logging.Logger;

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

public class Main {
    /** Used internally to create log information to detect errors. */
    private static final Logger LOGGER = Logger
            .getLogger("org.vfny.geoserver.global");

    /**
     * getChildText purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the first child text value
     * of the specified element name.
     * </p>
     *
     * @param root The root element to look for children in.
     * @param childName The name of the attribute to look for.
     *
     * @return The value if the child was found, the null otherwise.
     */
    public static String getChildText(Element root, String childName) {
        try {
            return getChildText(root, childName, false);
        } catch (Exception ex) {
            return null;
        }
    }

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

        if (elem != null) {
            return getElementText(elem, mandatory);
        } else {
            if (mandatory) {
                String msg = "Mandatory child " + childName
                        + "not found in " + " element: " + root;

                throw new Exception(msg);
            }

            return null;
        }
    }

    /**
     * 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;
        }
    }

    /**
     * getChildText purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the text value of the
     * specified element name.
     * </p>
     *
     * @param elem The root element to look for children in.
     *
     * @return The value if the text was found, the null otherwise.
     */
    public static String getElementText(Element elem) {
        try {
            return getElementText(elem, false);
        } catch (Exception ex) {
            return null;
        }
    }

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

        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(new StringBuffer("getting element text for ")
                    .append(elem).toString());
        }

        if (elem != null) {
            Node child;

            NodeList childs = elem.getChildNodes();

            int nChilds = childs.getLength();

            for (int i = 0; i < nChilds; i++) {
                child = childs.item(i);

                if (child.getNodeType() == Node.TEXT_NODE) {
                    value = child.getNodeValue();

                    if (mandatory && "".equals(value.trim())) {
                        throw new Exception(elem.getNodeName()
                                + " text is empty");
                    }

                    break;
                }
            }

            if (mandatory && (value == null)) {
                throw new Exception(elem.getNodeName()
                        + " element does not contains text");
            }
        } else {
            throw new Exception("Argument element can't be null");
        }

        return value;
    }
}

Related

  1. getChildText(Element parent, String childName)
  2. getChildText(Element parent, String childName)
  3. getChildText(Element parent, String childName)
  4. getChildText(Element parent, String kidName)
  5. getChildText(Element parent, String name)
  6. getChildText(Element tag, String childTagName)
  7. getChildText(final Element element, final String tagName)
  8. getChildText(final Element element, final String tagName)
  9. getChildText(final Element parentElem, final String childName)