Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * A safe way of getting attribute value of attribute with given name. If attribute with given name
     * doesn't exist, returns null
     * (instead of NPE).
     * 
     * @param node
     * @param attributeName
     * @return
     */
    public static String getAttribute(Node node, String attributeName) {
        if (node == null)
            return null;

        NamedNodeMap attributes = node.getAttributes();
        if (attributes == null)
            return null;

        Node attribute = attributes.getNamedItem(attributeName);
        if (attribute == null)
            return null;

        return attribute.getTextContent();
    }
}