Reputation: 41
I have a basic understanding of XML. My question is why is it necessary to mention the encoding used at the beginning of a XML document and why encoding is required ?
Upvotes: 4
Views: 3524
Reputation: 944527
why is it necessary to mention the encoding used at the beginning of a XML document
It isn't. There are defaults. (UTF-8 and UTF-16, which can be reliably distinguished between programatically)
and why encoding is required
Computers only understand binary. Encoding is the process of representing letters, numbers, etc in binary so it can be processed by a computer. Different encodings store the characters in different ways.
Upvotes: 1
Reputation: 35970
It is not required, although usually you may want to include it:
In the absence of external character encoding information (such as MIME headers), parsed entities which are stored in an encoding other than UTF-8 or UTF-16 must begin with a text declaration (see 4.3.1 The Text Declaration) containing an encoding declaration.
So, for example, when transferring XML via HTTP, XML parser might use the value from Content-Type
header like this:
Content-Type application/xml; charset=UTF-8
But once this document is stored locally, it would not contain this information - thus it seems like a good idea to include encoding into the declaration part of XML document.
Upvotes: 3