Java XML Attribute Get getAttrDate(Element elem, String localName)

Here you can find the source of getAttrDate(Element elem, String localName)

Description

Returns the Date value of the unqualified attribute with the given local name belonging to the given element, or null if the attribute is not present.

License

Open Source License

Parameter

Parameter Description
elem an element
localName an unqualified attribute name

Return

the Date value of the attribute, or null if the attribute is not present

Declaration

public static Date getAttrDate(Element elem, String localName) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .j av a  2 s  .c o m*/
 * Misc-Utils - Miscellaneous Utility Classes
 * Copyright (C) 2007 Newisys, Inc. or its licensors, as applicable.
 * Java is a registered trademark of Sun Microsystems, Inc. in the U.S. or
 * other countries.
 *
 * Licensed under the Open Software License version 3.0 (the "License"); you
 * may not use this file except in compliance with the License. You should
 * have received a copy of the License along with this software; if not, you
 * may obtain a copy of the License at
 *
 * http://opensource.org/licenses/osl-3.0.php
 *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import org.w3c.dom.Attr;

import org.w3c.dom.Element;

public class Main {
    private static final DateFormat dateFormat = DateFormat.getDateInstance();

    /**
    * Returns the Date value of the unqualified attribute with the given local
    * name belonging to the given element, or null if the attribute is not
    * present. The date format must match the default for the locale, as
    * returned by DateFormat.getDateInstance().
    * 
    * @param elem an element
    * @param localName an unqualified attribute name
    * @return the Date value of the attribute, or null if the attribute is not
    *         present
    */
    public static Date getAttrDate(Element elem, String localName) {
        String str = getAttrString(elem, localName);
        if (str != null) {
            try {
                return dateFormat.parse(str);
            } catch (ParseException ignored) {
            }
        }
        return null;
    }

    /**
    * Returns the String value of the unqualified attribute with the given
    * local name belonging to the given element, or null if the attribute is
    * not present.
    * 
    * @param elem an element
    * @param localName an unqualified attribute name
    * @return the String value of the attribute, or null if the attribute is
    *         not present
    */
    public static String getAttrString(Element elem, String localName) {
        Attr attr = elem.getAttributeNodeNS(null, localName);
        String value = (attr != null) ? attr.getValue() : null;
        return value;
    }
}

Related

  1. getAttr(Node n, String name)
  2. getAttr(Node node, String attr)
  3. getAttr(Node node, String name)
  4. getAttr(Node node, String name, String defaultValue)
  5. getAttrBool(Node n, String name)
  6. getAttrib(NamedNodeMap attribs, String name)
  7. getAttribAsBoolean(Element e, String name, Boolean dft)
  8. getAttribAsFloat(Element e, String name, Float dft)
  9. getAttribBoolean(Element ele, String name)