How to determine the encoding of an xml in java8

430 views Asked by At

I have an XML file for which I want to determine the encoding programmatically. The XML file is present on the device and the dataFilePath contains the path. The encoding declaration in the file says it is UTF-8 but when I check the encoding in Notepad ++ the file is ANSI encoded. Here's what I have tried

String encoding;
        FileReader reader1 = null;
        XMLStreamReader xmlStreamReader = null;
        reader1 = new FileReader(dataFilePath);
        xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(reader1);
        encoding = xmlStreamReader.getEncoding();
        InputStream inputStream = new FileInputStream(dataFilePath);
        Reader reader =
                new InputStreamReader(inputStream, Charset.forName(encoding));

encoding always returns null and an exception is raised

0

There are 0 answers