Check encoding of an XML

Asked

Viewed 625 times

2

I need to process a number of XML files. One of the requirements is that the encoding be it UTF-8. Any other kind of enconding must be rejected.

That is accepted:

<?xml version="1.0" encoding="UTF-8" ?>

That’s not

<?xml version="1.0" encoding="Qualquer outra coisa diferente de UTF-8" ?>

I’m using the javax.xml to read, validate and process my files but it is not mandatory. If you know another lib or method that makes it great!

I have already reviewed the internet and I couldn’t find anything like it. Have any of you ever had to do this? How did you solve?

  • 1

    @Kyllopardiun put more details, but the lib I’m using doesn’t make much difference, it could be anyone :p

  • 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.

1 answer

2


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> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.