-<?xml version =“ 1.0”的含义; encoding =“ utf-8”? [英] Meaning of - <?xml version="1.0" encoding="utf-8"?>

查看:605
本文介绍了-<?xml version =“ 1.0”的含义; encoding =“ utf-8”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XML的新手,我正在尝试了解基础知识。我在学习XML中阅读了下面的行,但是对我来说仍然不清楚。有人可以将我指向一本可以清楚地解释这些基本知识的书或网站吗?

I am new to XML and I am trying to understand the basics. I read the line below in "Learning XML", but it is still not clear, for me. Can someone point me to a book or website which explains these basics clearly?

学习XML


XML声明描述了文档
的一些最一般的属性,告诉XML处理器它需要XML解析器来解释

The XML declaration describes some of the most general properties of the document, telling the XML processor that it needs an XML parser to interpret this document.

这是什么意思?

我理解 xml版本部分-doc和doc用户都应该在相同版本的XML中交谈。但是 encoding 部分呢?为什么需要这样做?

I understand the xml version part - both doc and user of doc should "talk" in the same version of XML. But what about the encoding part? Why is that necessary?

推荐答案

要了解 encoding属性,您必须了解 bytes characters

To understand the "encoding" attribute, you have to understand the difference between bytes and characters.

将字节视为0到255之间的数字,而字符是 a, 1和Ä。可用的所有字符集称为字符集

Think of bytes as numbers between 0 and 255, whereas characters are things like "a", "1" and "Ä". The set of all characters that are available is called a character set.

每个字符都有一个或多个字节的序列,这些字节用于代表它但是,字节的确切数量和值取决于所使用的 encoding ,并且有许多不同的编码。

Each character has a sequence of one or more bytes that are used to represent it; however, the exact number and value of the bytes depends on the encoding used and there are many different encodings.

大多数编码基于旧的字符集和称为ASCII的编码,每个字符一个字节(实际上只有7位),包含128个字符,其中包括很多美国英语中常用的字符。

Most encodings are based on an old character set and encoding called ASCII which is a single byte per character (actually, only 7 bits) and contains 128 characters including a lot of the common characters used in US English.

例如,这是ASCII字符集中的6个字符,由值60到65表示。

For example, here are 6 characters in the ASCII character set that are represented by the values 60 to 65.

Extract of ASCII Table 60-65
╔══════╦══════════════╗
║ Byte ║  Character   ║
╠══════╬══════════════║
║  60  ║      <       ║
║  61  ║      =       ║
║  62  ║      >       ║
║  63  ║      ?       ║
║  64  ║      @       ║
║  65  ║      A       ║
╚══════╩══════════════╝

在完整的ASCII集中,使用的最小值是零,最大值是127(这两个都是隐藏的控制字符) )。

In the full ASCII set, the lowest value used is zero and the highest is 127 (both of these are hidden control characters).

但是,一旦您开始需要比基本ASCII所提供的字符更多的字符(例如,带有重音符号的字母,货币符号,图形符号等),则ASCII是不适合,您需要更广泛的功能。您需要更多的字符(不同的字符集),并且需要不同的编码,因为128个字符不足以容纳所有字符。某些编码提供一个字节(256个字符)或最多六个字节。

However, once you start needing more characters than the basic ASCII provides (for example, letters with accents, currency symbols, graphic symbols, etc.), ASCII is not suitable and you need something more extensive. You need more characters (a different character set) and you need a different encoding as 128 characters is not enough to fit all the characters in. Some encodings offer one byte (256 characters) or up to six bytes.

随着时间的流逝,已经创建了许多编码。在Windows世界中,有CP1252或ISO-8859-1,而Linux用户则倾向于使用UTF-8。 Java本机使用UTF-16。

Over time a lot of encodings have been created. In the Windows world, there is CP1252, or ISO-8859-1, whereas Linux users tend to favour UTF-8. Java uses UTF-16 natively.

在一种编码中某个字符的一个字节值序列可能代表在另一种编码中完全不同的字符,甚至可能无效。

One sequence of byte values for a character in one encoding might stand for a completely different character in another encoding, or might even be invalid.

例如,在 ISO 8859-1 中,â由值<$ c $的一个字节表示c> 226 ,而在 UTF-8 中则为两个字节: 195、162 。但是,在 ISO 8859-1 中, 195、162 是两个字符,Ã,¢

For example, in ISO 8859-1, â is represented by one byte of value 226, whereas in UTF-8 it is two bytes: 195, 162. However, in ISO 8859-1, 195, 162 would be two characters, Ã, ¢.

将XML看作不是字符序列而是字节序列。

Think of XML as not a sequence of characters but a sequence of bytes.

想象一下接收XML的系统看到了字节 195、162 。它如何知道这些字符是什么?

Imagine the system receiving the XML sees the bytes 195, 162. How does it know what characters these are?

为了使系统将这些字节解释为实际字符(并显示它们或将它们转换为其他编码),它会需要了解XML中使用的编码。

In order for the system to interpret those bytes as actual characters (and so display them or convert them to another encoding), it needs to know the encoding used in the XML.

由于大多数常见的编码都与ASCII兼容,因此在这种情况下,基本的字母字符和符号都需要声明。本身只能使用ASCII字符来说明编码是什么。在其他情况下,解析器必须尝试找出声明的编码。因为它知道声明以<?xml 开头,所以这样做要容易得多。

Since most common encodings are compatible with ASCII, as far as basic alphabetic characters and symbols go, in these cases, the declaration itself can get away with using only the ASCII characters to say what the encoding is. In other cases, the parser must try and figure out the encoding of the declaration. Since it knows the declaration begins with <?xml it is a lot easier to do this.

最后, version 属性指定XML版本,目前有两个版本(请参见 Wikipedia XML版本。版本之间存在细微差异,因此XML解析器需要知道它要处理的内容。在大多数情况下(无论如何,对于讲英语的人),1.0版是足够。

Finally, the version attribute specifies the XML version, of which there are two at the moment (see Wikipedia XML versions. There are slight differences between the versions, so an XML parser needs to know what it is dealing with. In most cases (for English speakers anyway), version 1.0 is sufficient.

这篇关于-&lt;?xml version =“ 1.0”的含义; encoding =“ utf-8”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆