Java XML Attribute Get getAttr(Node node, String name)

Here you can find the source of getAttr(Node node, String name)

Description

get Attr

License

Open Source License

Declaration

public static String getAttr(Node node, String name) throws DOMException 

Method Source Code


//package com.java2s;
/*/*from  w w w. ja  v  a  2 s  .  c om*/
AVR8 Burn-O-Mat
     
Copyright (C) 2007  Torsten Brischalle
    
This program 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 3 of the License, or
(at your option) any later version.
    
This program 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 program.  If not, see <http://www.gnu.org/licenses/
 */

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

public class Main {
    public static String getAttr(Node node, String name) throws DOMException {

        assert node != null;

        try {
            NamedNodeMap attributes = node.getAttributes();
            if (attributes == null)
                throw new Exception("");

            Node attr = attributes.getNamedItem(name);
            if (attr == null)
                throw new Exception("");

            String value = attr.getNodeValue();
            if (value == null)
                throw new Exception("");

            return value;

        } catch (Exception e) {
            throw new DOMException(DOMException.NOT_FOUND_ERR,
                    String.format("attribute %s is missing at node %s", name, node.getNodeName()));
        }

    }
}

Related

  1. getAttr(final Node n, final String attrName)
  2. getAttr(NamedNodeMap attrs, String name)
  3. getAttr(NamedNodeMap attrs, String name, String missing_err)
  4. getAttr(Node n, String name)
  5. getAttr(Node node, String attr)
  6. getAttr(Node node, String name, String defaultValue)
  7. getAttrBool(Node n, String name)
  8. getAttrDate(Element elem, String localName)
  9. getAttrib(NamedNodeMap attribs, String name)