Just check the encoding by the method getEncoding()
present in the XMLStreamReader
:
Below a complete example:
import java.io.InputStream;
import java.net.URL;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class LeitorXML {
public boolean isUTF8(InputStream entrada) throws XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader = factory.createXMLStreamReader(entrada);
System.out.println(xmlReader.getEncoding());
return xmlReader.getEncoding().equalsIgnoreCase("UTF-8");
}
public static void main(String[] args) {
LeitorXML reader = new LeitorXML();
try {
URL url = LeitorXML.class.getClassLoader().getResource("exemplo2.xml");
InputStream strm=null;
strm = url.openStream();
if(reader.isUTF8(strm)){
System.out.println("O documento é UTF-8");
}else{
System.out.println("O documento não é UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
example.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<item date="2009">
<mode>1</mode>
</item>
<item date="2010">
<mode>2</mode>
</item>
</config>
for example2.xml
<?xml version="1.0" encoding="UTF-16"?>
<config>
<item date="2009">
<mode>1</mode>
</item>
<item date="2010">
<mode>2</mode>
</item>
</config>
@Kyllopardiun put more details, but the lib I’m using doesn’t make much difference, it could be anyone :p
– Edgar Muniz Berlinck
Edgar, is that for each of the libraries there is a way that is better than the other by better integrating with the rest of the code.
– Mansueli