Java XML Attribute Get getAttributeByIndex(final Element element, final int index)

Here you can find the source of getAttributeByIndex(final Element element, final int index)

Description

get Attribute By Index

License

Open Source License

Declaration

public static String getAttributeByIndex(final Element element, final int index) 

Method Source Code


//package com.java2s;
/*//from  w w w .  j  av a  2s  . 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.ArrayList;
import java.util.List;

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

public class Main {
    public static String getAttributeByIndex(final Element element, final int index) {
        return getAttribute(getElementsFromNodeList(element.getChildNodes()), index);
    }

    private static String getAttribute(final List<Element> elements, final int index) {
        final Element element = elements.get(index);
        final NamedNodeMap attrs = element.getAttributes();

        final Node item = attrs.getNamedItem("value");
        if (item != null) {
            return item.getNodeValue();
        }

        return null;
    }

    public static List<Element> getElementsFromNodeList(final NodeList nodeList) {
        final List<Element> elements = new ArrayList<>();

        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node node = nodeList.item(i);
            if (node instanceof Element) {
                elements.add((Element) node);
            }
        }

        return elements;
    }
}

Related

  1. getAttributeBoolean(final Node node, final String name, final Boolean defaultValue)
  2. getAttributeBoolean(Node node, String attributeName)
  3. getAttributeBoolean(Node node, String name)
  4. getAttributeBooleanByName(NamedNodeMap nnm, String name)
  5. getAttributeBooleanByName(NamedNodeMap nnm, String name)
  6. getAttributeByLocalName(XMLStreamReader reader, String localName)
  7. getAttributeByName(final List elements, final String attributeName)
  8. getAttributeByName(Node content, String attributeName)
  9. getAttributeByName(Node node, String name, boolean defaultValue)