on 2010年11月21日星期日 | 0 评论

Blog topic
What is YAML? What are its advantages over XML? Give examples on how an XML document can be represented by a YAML document.

YAML Ain't Markup Language. According to the Wikipedia, the YAML is a human-readable data serialization format that takes concepts from programming languages such as C, Perl, and Python, and ideas from XML and the data format of electronic mail.

Comparing with the XML, first, the grammer of YAML is much easier than the XML, Representing simple, hierarchical data tends to be more gracefully done in YAML; Secondly, XML is meant to be a markup language and YAML is really more of a data format; YAML largely eliminates the latter's perceived line noise such as brackets and braces. I will give a code example to compare XML with YAML. YAML is more readable than XML. YAML is expressive and extensible and it is easy to implement and use.

Showing a simple example of YAML, the following file name is Tim.YAML. This fragment of the code is very readable, it describes the Tim Zhao is 55 years old and has a 50 years old wife called Gimmy, they have two children one is Jimmy Zhao and the other is Jenny Zhao.

Block sequences indicate each entry with a dash and space ( ), the “– name” is the block sequence.

Mappings use a colon and space () to mark each key: value pair. “Name: Tim Zhao” is the Mapping values.

name: Tim Zhao
age: 55
spouse:
    name: Emmy
    age: 50
children:
    - name: Jimmy Zhao
        age: 25
    - name: Jenny Zhao
        age 22


YAML also has some disadvantages eompare with XML, such as If you need to transform an XML document to another format (HTML) you can use XSLT while for YAML you have to write a program; Only a few major programming languages have a proper support for YAML; Java and Python have XML support in the standard libraries. YAML (for Java and Python) requires an external dependency;

The XML document can be represented by a YAML, I wll list a simple example to illustrate it.
The XML code:
<?xml version="1.0"?>
<club>
  <players>
    <player id="kramnik"
            name="Vladimir Kramnik"
            rating="2700"
            status="GM" />
    <player id="fritz"
            name="Deep Fritz"
            rating="2700"
            status="Computer" />
    <player id="mertz"
            name="David Mertz"
            rating="1400"
            status="Amateur" />
  </players>
  <matches>
    <match>
        <Date>2002-10-04</Date>
        <White refid="fritz" />
        <Black refid="kramnik" />
        <Result>Draw</Result>
    </match>
    <match>
        <Date>2002-10-06</Date>
        <White refid="kramnik" />
        <Black refid="fritz" />
        <Result>White</Result>
    </match>
  </matches>
</club>
The above XML data representation is fairly clear. It is not all that difficult to modify the document with general purpose tools like a text editor. Semantically, my proposed XML has all the problems discussed. Players appear ordered. And the player list appears to precede the matches list. Player attributes are unordered, but since match "attributes" cannot fit as XML attributes, XML imposes an artificial order.[3]


The YAML code: The YAML format simply matches the data structures of dynamic languages better.
players:
  Vladimir Kramnik: &kramnik
    rating: 2700
    status: GM
  Deep Fritz: &fritz
    rating: 2700
    status: Computer
  David Mertz: &mertz
    rating: 1400
    status: Amateur
 
matches:
  -
    Date: 2002-10-04
    White: *fritz
    Black: *kramnik
    Result: Draw
  -
    Date: 2002-10-06
    White: *kramnik
    Black: *fritz
Result: White
 
There are a number of nice things about this format. The YAML Web site gives exact specifications, but this brief sample gives a pretty accurate idea of the basic elements. YAML is terse, and readable. Moreover, quoting is minimal, with data types being inferred from patterns. You can use references to any named target. And, significantly, YAML maintains the distinction between ordered and associative collections. As an added bonus, you can very easily edit YAML in a text editor.[3]

I just listed some obvious and simple concept and advantages of the YAML, there are still a lot of good features not be covered in this essay. YAML is a human-friendly, cross language, Unicode based data serialization language. It has some advantages that XML doesn’t covered, but YAML cannot replace the XML right now. We expect that the YAML could make a good progress in the future.


References:

[1]. YAML, [online], last accesssed on November 17, 2010 http://www.yaml.org/spec/1.2/spec.html
[2]. YAML compared to XML, [online], last accesssed on November 17, 2010 http://stackoverflow.com/questions/1308536/yaml-compared-to-xml
[3] YAML improves on XML, [online], last accesssed on November 17, 2010 http://www.ibm.com/developerworks/xml/library/x-matters23.html


0 评论:

发表评论