Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;
import java.net.URL;

public class Main {
    public static String getDefaultNamespaceUri(URL xmlResource, String rootElementName)
            throws IOException, ParserConfigurationException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlResource.openStream());
        NodeList nodes = document.getElementsByTagName(rootElementName);
        if (nodes == null || nodes.getLength() == 0) {
            throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \""
                    + xmlResource.toExternalForm() + "\".");
        }
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Node xmlns = node.getAttributes().getNamedItem("xmlns");
            if (xmlns != null) {
                String value = xmlns.getNodeValue();
                return value.substring(value.indexOf("=") + 1);
            }
        }
        return null;
    }
}