Example usage for org.w3c.dom Document getNamespaceURI

List of usage examples for org.w3c.dom Document getNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Document getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

From source file:org.opennms.protocols.xml.collector.AbstractXmlCollectionHandler.java

/**
 * Gets the XML document./*w w  w. java  2 s. com*/
 *
 * @param is the input stream
 * @param request the request
 * @return the XML document
 * @throws Exception the exception
 */
protected Document getXmlDocument(InputStream is, Request request) throws Exception {
    is = preProcessHtml(request, is);
    is = applyXsltTransformation(request, is);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String contents = writer.toString();
    Document doc = builder.parse(IOUtils.toInputStream(contents, "UTF-8"));
    // Ugly hack to deal with DOM & XPath 1.0's battle royale 
    // over handling namespaces without a prefix. 
    if (doc.getNamespaceURI() != null && doc.getPrefix() == null) {
        factory.setNamespaceAware(false);
        builder = factory.newDocumentBuilder();
        doc = builder.parse(IOUtils.toInputStream(contents, "UTF-8"));
    }
    return doc;
}