Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import org.apache.log4j.Logger;

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

public class Main {
    private static Logger logger = Logger.getLogger(new Exception().getStackTrace()[0].getClassName());

    public static void printNodeList(NodeList nodeList) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            buffer.append(node.getNodeName() + " = " + node.getNodeValue() + " Attributes: " + attributesStr(node)
                    + "\n");
        }
        logger.info(buffer);
    }

    public static void printNodeList(ArrayList<Node> nodeList) {
        StringBuffer buffer = new StringBuffer();
        for (Node node : nodeList) {
            buffer.append(node.getNodeName() + " = " + node.getNodeValue() + " Attributes: " + attributesStr(node)
                    + "\n");
        }
        logger.info(buffer);
    }

    public static String attributesStr(Node node) {
        if (node == null) {
            return null;
        }
        StringBuffer attributes = new StringBuffer();
        for (int i = 0; i < node.getAttributes().getLength(); i++) {
            attributes.append(node.getAttributes().item(i).getNodeName() + "="
                    + node.getAttributes().item(i).getNodeValue() + ", ");
        }
        if (attributes.length() > 1) {
            attributes.delete(attributes.length() - 2, attributes.length());
        } else {
            attributes.append(node.getNodeName() + " has NO attributes.");
        }
        return attributes.toString();
    }
}