Java XML Namespace getBaseNamespace(InputStream owlStream)

Here you can find the source of getBaseNamespace(InputStream owlStream)

Description

get Base Namespace

License

Open Source License

Declaration

public static String getBaseNamespace(InputStream owlStream) 

Method Source Code

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

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
    public static String getBaseNamespace(InputStream owlStream) {
        String base = "";
        try {//from   w w  w  .  j av  a  2 s.c o m
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(owlStream);
            Element root = doc.getDocumentElement();
            //get root name space
            NamedNodeMap attributes = root.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Node node = attributes.item(i);
                    if (node != null) {
                        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                            String name = node.getNodeName();
                            if (name.equals("xmlns"))
                                base = node.getNodeValue();
                            else if (name.equals("xml:base"))
                                base = node.getNodeValue() + "#";
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return base;
    }
}

Related

  1. compile(String expression, NamespaceContext namespaceContext)
  2. createNamespaceContext(final String nsPrefix, final String nsUri)
  3. createXMLReader(boolean validating, boolean namespaceAware)
  4. createXMLReader(boolean validating, boolean withNamespace)
  5. getBuilder(boolean ignoreComments, boolean validating, boolean ignoreContentWhitespace, boolean isNamespaceAware)
  6. getCarbonNamespace()
  7. getDefaultNamespaceURI(final XMLStreamWriter writer)
  8. getFactory(boolean validate, boolean namespaceAware)