Java Utililty Methods XML Namespace

List of utility methods to do XML Namespace

Description

The list of methods to do XML Namespace are organized into topic(s).

Method

XPathExpressioncompile(String expression, NamespaceContext namespaceContext)
Compiles an XPath expression for later evaluation.
return createXPath(namespaceContext, null).compile(expression);
NamespaceContextcreateNamespaceContext(final String nsPrefix, final String nsUri)
create Namespace Context
return new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        if (prefix.equals(nsPrefix)) {
            return nsUri;
        } else {
            return XMLConstants.NULL_NS_URI;
    public String getPrefix(String namespace) {
        if (namespace.equals(nsUri)) {
            return nsPrefix;
        } else {
            return null;
    public Iterator<String> getPrefixes(String namespace) {
        return null;
};
XMLReadercreateXMLReader(boolean validating, boolean namespaceAware)
This method attempts to use JAXP to locate the SAX2 XMLReader implementation.
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(validating);
factory.setNamespaceAware(namespaceAware);
SAXParser parser = factory.newSAXParser();
return parser.getXMLReader();
XMLReadercreateXMLReader(boolean validating, boolean withNamespace)
Create an XMLReader instance.
try {
    synchronized (parserFactory) {
        parserFactory.setValidating(validating);
        parserFactory.setNamespaceAware(withNamespace);
        return parserFactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException e) {
    throw new SAXException(e);
...
StringgetBaseNamespace(InputStream owlStream)
get Base Namespace
String base = "";
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(owlStream);
    Element root = doc.getDocumentElement();
    NamedNodeMap attributes = root.getAttributes();
...
DocumentBuildergetBuilder(boolean ignoreComments, boolean validating, boolean ignoreContentWhitespace, boolean isNamespaceAware)
get Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(ignoreComments);
factory.setValidating(validating);
factory.setIgnoringElementContentWhitespace(ignoreContentWhitespace);
factory.setNamespaceAware(isNamespaceAware);
DocumentBuilder builder = null;
try {
    builder = factory.newDocumentBuilder();
...
NamespaceContextgetCarbonNamespace()
get Carbon Namespace
NamespaceContext ctx = new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        return "http://wso2.org/projects/carbon/carbon.xml";
    public String getPrefix(String arg0) {
        return null;
    public Iterator getPrefixes(String namespaceURI) {
...
StringgetDefaultNamespaceURI(final XMLStreamWriter writer)
get Default Namespace URI
return writer.getNamespaceContext().getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
DocumentBuilderFactorygetFactory(boolean validate, boolean namespaceAware)
get Factory
DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1];
if (factory == null) {
    factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);
    doms[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory;
return factory;
...
StringgetNamespace(Class clazz, String namespace)
get Namespace
if (namespace == null || namespace.trim().length() == 0) {
    Package pkg = clazz.getPackage();
    if (pkg == null) {
        return null;
    } else {
        return getNamespace(pkg.getName());
} else {
...