Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have noticed that Linux file command does not return any value if an xml file has the <?xml version="1.0" encoding="UTF-8"?> at the top. If the prolog is missing then file -i somefile.xml returns some value. Does anyone has any idea why this happens? I am basically interested in knowing the file encoding and have found file -i filename.xml to be somewhat useful, but is there anything better available on Linux to figure the encoding and charset?

share|improve this question

migrated from Jun 25 '10 at 20:38

This question came from our site for professional and enthusiast programmers.

File relies on the signature in the magic file, it is quite certain that it is looking at the prolog only, not scanning parts of the file to keep it simple.

share|improve this answer
That is what I wondering that maybe it just looks at the prolog which isn't really helpful because I can encode the file in X and write Y in the prolog – Anonymous Jun 25 '10 at 20:37

Guessing encodings is a black art that even popular browsers can't get exactly right. I suggest you let your parser obey the XML encoding, breaking loudly if the encoding turns out to be invalid, and that you fix encoding mistakes at the source.

share|improve this answer

You must log in to answer this question.