factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
if (!data.startsWith("<?xml")) {
data = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + data;
}
return builder.parse(new StringInputStream(data));
} catch (Throwable t) {
LOGGER.log(Level.FINE, "Failed to parse XML, assuming it's plain text", t);
return null;
}
}