Java XML Attribute Get getAttributeByName(final List elements, final String attributeName)

Here you can find the source of getAttributeByName(final List elements, final String attributeName)

Description

get Attribute By Name

License

Open Source License

Declaration

private static String getAttributeByName(final List<Element> elements, final String attributeName) 

Method Source Code

//package com.java2s;
/*/*from   w  ww. j  ava  2  s .  c  o m*/
* ===========================================
* PDF Forms Designer
* ===========================================
*
* Project Info:  http://pdfformsdesigne.sourceforge.net
* (C) Copyright 2006-2008..
* Lead Developer: Simon Barnett (n6vale@googlemail.com)
*
*  This file is part of the PDF Forms Designer
*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
    
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
    
*
* ---------------
* XMLUtils.java
* ---------------
*/

import java.util.List;

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

public class Main {
    private static String getAttributeByName(final List<Element> elements, final String attributeName) {
        for (final Element element : elements) {
            final String value = getAttributeFromElement(element, attributeName);
            if (value != null) {
                return value;
            }
        }

        return null;
    }

    public static String getAttributeFromElement(final Element element, final String attributeName) {
        final NamedNodeMap attrs = element.getAttributes();

        final Node nameNode = attrs.getNamedItem("name");
        if (nameNode != null) {
            final String nodeValue = nameNode.getNodeValue();
            if (nodeValue != null) {
                if (nodeValue.equals(attributeName)) {
                    final Node namedItem = attrs.getNamedItem("value");
                    if (namedItem != null) {
                        return namedItem.getNodeValue();
                    }
                }
            }
        }

        return null;
    }
}

Related

  1. getAttributeBoolean(Node node, String name)
  2. getAttributeBooleanByName(NamedNodeMap nnm, String name)
  3. getAttributeBooleanByName(NamedNodeMap nnm, String name)
  4. getAttributeByIndex(final Element element, final int index)
  5. getAttributeByLocalName(XMLStreamReader reader, String localName)
  6. getAttributeByName(Node content, String attributeName)
  7. getAttributeByName(Node node, String name, boolean defaultValue)
  8. getAttributeByName(Node node, String name, String defaultValue)
  9. getAttributeContent(Node item, String attributeName)