get XML DOM Attr by name - Java XML

Java examples for XML:XML Attribute

Description

get XML DOM Attr by name

Demo Code


//package com.java2s;

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

public class Main {
    public static String getAttr(NamedNodeMap attrs, String name) {
        return getAttr(attrs, name, null);
    }/*from  w  w w  .  jav  a2s .  c  om*/

    public static String getAttr(Node nd, String name) {
        return getAttr(nd.getAttributes(), name);
    }

    public static String getAttr(NamedNodeMap attrs, String name,
            String missing_err) {
        Node attr = attrs == null ? null : attrs.getNamedItem(name);
        if (attr == null) {
            if (missing_err == null)
                return null;
            throw new RuntimeException(missing_err
                    + ": missing mandatory attribute '" + name + "'");
        }
        String val = attr.getNodeValue();
        return val;
    }

    public static String getAttr(Node node, String name, String missing_err) {
        return getAttr(node.getAttributes(), name, missing_err);
    }
}

Related Tutorials