Example usage for org.w3c.dom Document getPrefix

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

Introduction

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

Prototype

public String getPrefix();

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

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

/**
 * Gets the XML document.//from  w w  w.ja v a  2s .co  m
 *
 * @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;
}