Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

import org.w3c.dom.*;

public class Main {
    /**
     * Returns whether the given XML DOM node contains all of the given attributes.
     * @param node the XML node to examine
     * @param names a variable-length list of attribute names to check
     * @return true if all attributes exist, false if not
     */
    public static boolean hasAttributes(Node node, String... names) {
        NamedNodeMap attrs = node.getAttributes();
        for (String name : names) {
            if (attrs.getNamedItem(name) == null) {
                return false;
            }
        }

        return true;
    }

    /**
     * Returns all attributes of the given XML node as a list of name=value strings.
     * Mostly just for debugging.
     * @param node
     * @return
     */
    public static List<String> getAttributes(Node node) {
        List<String> result = new ArrayList<String>();
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            result.add(attrNode.getNodeName() + "=" + attrNode.getNodeValue());
        }
        return result;
    }
}