Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Rails for Java Developers phần 9 pot
Nội dung xem thử
Mô tả chi tiết
YAML AND XML COMPARED 262
XML is intended to be human-readable and self-describing. XML is
human-readable because it is a text format, and it is self-describing
because data is described by elements such as <user>, <username>, elements
and <homepage> in the preceding example. Another option for representing usernames and home pages would be XML attributes:
<user username="stu" homepage="http://blogs.relevancellc.com"></user>
The attribute syntax is obviously more terse. It also implies semantic differences. Attributes are unordered, while elements are ordered.
Attributes are also limited in the values they may contain: Some characters are illegal, and attributes cannot contain nested data (elements,
on the other hand, can nest arbitrarily deep).
There is one last wrinkle to consider with this simple XML document.
What happens when it travels in the wide world and encounters other
elements named <user>? To prevent confusion, XML allows namespaces. These serve the same role as Java packages or Ruby modules, namespaces
but the syntax is different:
<rel:user xmlns:rel="http://www.relevancellc.com/sample"
username="stu"
homepage="http://blogs.relevancellc.com">
</rel:user>
The namespace is http://www.relevancellc.com/sample. That would be a
lot to type in front of an element name, so xmlns:rel establishes rel as a
prefix. Reading the previous document, an XML wonk would say that
<user> is in the http://www.relevancellc.com/sample namespace.
YAML is a response to the complexity of XML (YAML stands for YAML
Ain’t Markup Language). YAML has many things in common with XML.
Most important, both YAML and XML can be used to represent and serialize complex, nested data structures. What special advantages does
YAML offer?
The YAML criticism of XML boils down to a single sentence. XML has
two concepts too many:
• There is no need for two different forms of nested data. Elements
are enough.
• There is no need for a distinct namespace concept; scoping is sufficient for namespacing.
To see why attributes and namespaces are superfluous in YAML, here
are three YAML variants of the same configuration file:
YAML AND XML COMPARED 263
Download code/rails_xt/samples/why_yaml.rb
user:
username: stu
homepage: http://blogs.relevancellc.com
As you can see, YAML uses indentation for nesting. This is more terse
than XML’s approach, which requires a closing tag.
The second XML example used attributes to shorten the document to a
single line. Here’s the one-line YAML version:
Download code/rails_xt/samples/why_yaml.rb
user: {username: stu, homepage: http://blogs.relevancellc.com}
The one-line syntax introduces {} as delimiters, but there is no semantic
distinction in the actual data. Name/value data, called a simple mapping in YAML, is identical in the multiline and one-line documents. simple mapping
Here’s a YAML “namespace”:
Download code/rails_xt/samples/why_yaml.rb
http://www.relevancellc.com/sample:
user: {username: stu, homepage: http://blogs.relevancellc.com}
There is no special namespace construct in YAML, because scope provides a sufficient mechanism. In the previous document, user belongs
to http://www.relevancellc.com/sample. Replacing the words “belongs to”
with “is in the namespace” is a matter of taste.
It is easy to convert from YAML to a Ruby object:
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML.load("{username: stu}")
=> {"username"=>"stu"}
Or from a Ruby object to YAML:
irb(main):003:0> YAML.dump 'username'=>'stu'
=> "--- \nusername: stu"
The leading - – \n: is a YAML document separator. This is optional, and
we won’t be using it in Rails configuration files. See the sidebar on the
next page for pointers to YAML’s constructs not covered here.
Items in a YAML sequence are prefixed with ’- ’:
- one
- two
- three