Java XML Attribute Get getAttributeValue(Element e, String name)

Here you can find the source of getAttributeValue(Element e, String name)

Description

Get an attribute value

License

Open Source License

Parameter

Parameter Description
e a parameter
name a parameter

Declaration

static public String getAttributeValue(Element e, String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Vladimir Silva and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  www .j ava  2 s.c  o m
 *     Vladimir Silva - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Get an attribute value
     * @param e
     * @param name
     * @return
     */
    static public String getAttributeValue(Element e, String name) {
        Node n = e.getAttributes().getNamedItem(name);
        return (n != null) ? n.getNodeValue() : null;

    }

    /**
     * Extract the value of a XML element  
     * @param e Document element
     * @param name
     * @return
     */
    static public String getNodeValue(Element e, String name) {
        NodeList nl = e.getElementsByTagName(name);

        try {
            return (nl != null && nl.getLength() > 0) ? nl.item(0).getFirstChild().getNodeValue().trim() : null;

        } catch (NullPointerException ex) {
            return null;
        }
    }
}

Related

  1. getAttributeText(final Node node, final String name)
  2. getAttributeTextContent(Node node, String attiribute_name)
  3. getAttributeUri(Element leaf, Element parent, String defaultBaseUri)
  4. getAttributeValue(@Nonnull final Element aElement, @Nonnull final String sAttrName)
  5. getAttributeValue(Element e, String key)
  6. getAttributeValue(Element el, String attributeName)
  7. getAttributeValue(Element el, String attributeName)
  8. getAttributeValue(Element el, String attrName)
  9. getAttributeValue(Element ele, String attrName)