String uri = "namespaceuri";
String prefix = "nsp";
String check = "xmlns:" + prefix + "='" + uri + "'";
TransformerHandler handler = factory.newTransformerHandler();
handler.setResult(new StreamResult(writer));
handler.getTransformer().setOutputProperties(format);
// Output a single element
handler.startDocument();
handler.startPrefixMapping(prefix, uri);
handler.startElement(uri, "element", "", new AttributesImpl());
handler.endPrefixMapping(prefix);
handler.endDocument();
String text = writer.toString();
// Check if the namespace is there (replace " by ' to be sure of what we search in)
boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1);
String msg = needsIt ? " needs namespace attributes (will be slower)." :
" handles correctly namespaces.";
getLogger().debug("Trax handler " + handler.getClass().getName() + msg);
this.needsNamespaceCache.put(factory.getClass().getName(), new Boolean(needsIt));
return needsIt;
}