Questo funziona per varie codifiche, tenendo riguardano sia la distinta base e la dichiarazione XML. Il valore predefinito è UTF-8
se non si applica:
String encoding;
FileReader reader = null;
XMLStreamReader xmlStreamReader = null;
try {
InputSource is = new InputSource(file.toURI().toASCIIString());
XMLInputSource xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), null);
xis.setByteStream(is.getByteStream());
PropertyManager pm = new PropertyManager(PropertyManager.CONTEXT_READER);
for (Field field : PropertyManager.class.getDeclaredFields()) {
if (field.getName().equals("supportedProps")) {
field.setAccessible(true);
((HashMap<String, Object>) field.get(pm)).put(
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY,
new XMLErrorReporter());
break;
}
}
encoding = new XMLEntityManager(pm).setupCurrentEntity("[xml]".intern(), xis, false, true);
if (encoding != "UTF-8") {
return encoding;
}
// From @matthias-heinrich’s answer:
reader = new FileReader(file);
xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(reader);
encoding = xmlStreamReader.getCharacterEncodingScheme();
if (encoding == null) {
encoding = "UTF-8";
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new UndeclaredThrowableException(e);
} finally {
if (xmlStreamReader != null) {
try {
xmlStreamReader.close();
} catch (XMLStreamException e) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return encoding;
provata su Java 6 con:
UTF-8
file XML con distinta base, con la dichiarazione XML ✓
UTF-8
file XML senza BOM, con dichiarazione XML ✓
UTF-8
File XML con BOM, senza dichiarazione XML ✓
UTF-8
File XML senza BOM, senza XML Dichiarazione ✓
ISO-8859-1
file XML (senza BOM), con dichiarazione XML ✓
UTF-16LE
file XML con BOM, senza dichiarazione XML ✓
UTF-16BE
file XML con BOM, senza dichiarazione XML ✓
Standing sulle spalle di questi giganti:
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.xml.stream.*;
import org.xml.sax.*;
import com.sun.org.apache.xerces.internal.impl.*;
import com.sun.org.apache.xerces.internal.xni.parser.*;
'org.w3c.dom.Document.getXmlEncoding()' ?? – artbristol
Anche se questo è vecchio: c'è una dichiarazione W3C ufficiale: https://www.w3.org/TR/xml/#sec-guessing –