Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Set;
import java.util.TreeSet;

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

public class Main {
    private static Set<String> getTree(Node doc) {
        final Set<String> set = new TreeSet<String>();
        if (doc == null)
            return set;

        String path = new String();
        if (doc.getPrefix() != null && doc.getLocalName() != null) {
            path = new String(doc.getPrefix() + ":" + doc.getLocalName());
            set.add(path);
        }

        final NamedNodeMap attributes = doc.getAttributes();
        for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
            final Node attribute = attributes.item(i);
            String name = attribute.getLocalName();
            if (attribute.getPrefix() != null)
                name = attribute.getPrefix() + ":" + name;
            set.add(path + "/@" + name);
        }

        final NodeList nodes = doc.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            final Node node = nodes.item(i);
            if (node instanceof Element) {
                final Set<String> children = getTree(node);
                for (final String child : children) {
                    set.add(path + "/" + child);
                }
            }
        }
        return set;
    }
}