How XML and Yaml are used _xml/rss

Source: Internet
Author: User
Tags dsn html tags advantage

Recently processed some configuration files, encountered in the YAML format of the file, because no previous contact with this format file, for XML is more familiar. So what is YAML? What are the advantages and disadvantages of it compared to XML? What is the situation with YAML? Yaml How to use it? Make a simple summary here. Let's start with the XML.
Believe in XML, everyone is familiar with it. Here are the conceptual things I've picked up from the Web, and you can take a look. I don't have to say much about it here. Say a few more basic uses.
XML Overview:
Extensible Markup Language (extensible Markup Language, XML), which is used to mark an electronic file with a structured markup language that can be used to mark data, define data types, and is a source language that allows users to define their own markup language. XML is a subset of standard Universal Markup Language (SGML) that is ideal for Web transport. XML provides a uniform way to describe and exchange structured data that is independent of applications or vendors.
Format attributes:
Unlike databases such as access,oracle and SQL Server, the database provides more robust data storage and analysis capabilities, such as data indexing, sorting, lookup, correlation consistency, and so on, and XML is just storing data. In fact, the biggest difference between XML and other data representations is that it's extremely simple, a seemingly trivial advantage, but it makes XML unique.
The difference between XML and HTML design is that XML is designed to transmit and store data, and its focus is on the content of the data. HTML is designed to display data, and the focus is on the appearance of the data. HTML is designed to display information, and XML is designed to transmit information.
XML and HTML syntax differences: HTML tags are not all required to appear in pairs, XML requires all tags must be in pairs, HTML tags are case-insensitive, XML is size sensitive, that is, case-sensitive.
Write:
XML read and write I am familiar with two ways, one is through JavaScript to get the XML value, one is read in PHP. XML writing You can refer to the manual, XML format is relatively free, you can customize the label, but there is a principle to be intuitive. The following directly listed examples for everyone to test, if there are problems you can communicate.

Note.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<note>
<from>John</from>
<to>George</to>
<message>don ' t forget the meeting!</message>
</note>

Xml_test.html
Javascript
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<body>
<p>
<b>To:</b> <span id= "to" ></span><br/>
<b>From:</b> <span id= "from" ></span><br/>
<b>Message:</b> <span id= "message" ></span>
</p>
<script type= "Text/javascript" >
if (window. XMLHttpRequest)
{//code for ie7+, Firefox, Chrome, Opera, Safari
Xmlhttp=new XMLHttpRequest ();
}
Else
{//code for IE6, IE5
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
Xmlhttp.open ("Get", "Note.xml", false);
Xmlhttp.send ();
Xmldoc=xmlhttp.responsexml;

document.getElementById ("to"). innerhtml=
Xmldoc.getelementsbytagname ("to") [0].childnodes[0].nodevalue;
document.getElementById ("from"). innerhtml=
Xmldoc.getelementsbytagname ("from") [0].childnodes[0].nodevalue;
document.getElementById ("message"). innerhtml=
Xmldoc.getelementsbytagname ("message") [0].childnodes[0].nodevalue;
</script>

</body>

xml_test.php
Copy Code code as follows:

<?php
Creating a DOM Object
$xml = new DOMDocument ();
reading XML files
$xml = simplexml_load_file (' Note.xml ');
Output the From attribute in the XML file, multiple identical attributes, which are the form of an array, can be evaluated with subscript
echo $xml->from;
?>

Yaml Introduction:
YAML, as a more simple and readable serialization language than XML, is increasingly being used in the development of applications and configuration files. This paper will briefly introduce the present situation of YAML, YAML the advantages and disadvantages compared with the XML, and give the typical application scenarios of YAML and its usage (including C + +, Ruby, PHP, etc.) through practical examples.
Advantage:
Yaml is good for readability.
Yaml and scripting languages are very interactive.
YAML uses the data type of the implementing language.
YAML has a consistent information model.
Yaml is easy to implement.
The above 5 is where XML is deficient. At the same time, YAML also has the following advantages of XML:
Yaml can be processed based on flow;
Yaml expressive ability and good expansibility.
In short, Yaml tried to accomplish the task of XML in a more agile way than XML.
See http://www.yaml.org for more content and specifications.
Grammar:
Structure is shown by a space. The items in the sequence are represented by "-", and the key value pairs in the map are delimited by ":".
That's almost all the syntax.
Like what......
The general yaml file name extension is. yaml. For example: John.yaml
Copy Code code as follows:

Name:john Smith

Age:37

Spouse:

Name:jane Smith

Age:25

Children

-Name:jimmy Smith

Age:15

-Name:jenny Smith

Age 12

John is 37 years old and has a happy family of four. Two children Jimmy and Jenny are lively and lovely. Wife Jane is young and beautiful.
If you study it in depth, you may find some social problems ^_^.
Visible Yaml readability is good.
Write:
PHP for Yaml Reading and writing, I recommend using SPYC class to read and write Yaml files.
Spyc class file Download address:
https://github.com/mustangostang/spyc/

SPYC only 2 classes of methods are available, one is to read the Yaml file, and one is to generate the Yaml file format. Here are two ways to do this.
Copy Code code as follows:

Include (' spyc.php ');

Reading Yaml files, generating arrays
$yaml = Spyc::yamlload (' Spyc.yaml ');

Convert an array to a YAML file
$array [' name '] = ' Andy ';
$array [' site '] = ' 21andy.com ';
$yaml = Spyc::yamldump ($array);

PHP.ini Read INI parsing method that configuration cannot support multidimensional arrays, so, I am interested in yaml to generate multidimensional arrays, I would like to do the configuration file, as follows:

Copy Code code as follows:

-{row:0, col:0, func: {tx: [0, 1]}}

Convert to PHP multidimensional array as follows:
Test.yaml (This example is my DB configuration file, strongly recommended!) )
Copy Code code as follows:

bb|
Default
DSN: ' mysql:dbname=test;host=127.0.0.1 '
User: ' Root '
Pass: ' 111 '
Session
DSN: ' mysql:dbname=test;host=127.0.0.1 '
User: ' Root '
Pass: ' 111 '

test.php
Copy Code code as follows:

<?php
Include (' spyc.php ');
Reading Yaml files, generating arrays
$yaml = Spyc::yamlload (' Test.yaml ');
echo "<pre>";
Print_r ($YAML);
echo "</pre>";
PHP Code
Array
(
[DB] => Array
(
[Default] => Array
(
[DSN] => mysql:dbname=test;host=127.0.0.1
[User] => root
[Pass] => 111
)

[Session] => Array
(
[DSN] => mysql:dbname=test;host=127.0.0.1
[User] => root
[Pass] => 111
)

)

)

PHP generates YAML file example:
<?php
Include (' spyc.php ');
Convert an array to YAML file format
$array [' name '] = ' PHP program Ape's Notes ';
$array [' site '] = ' www.songchaoke.cn ';
$yaml = Spyc::yamldump ($array);
Writes the converted Yaml to a file
$f = fopen (' Test2.yaml ', "w+");
Fwrite ($f, $yaml);
Fclose ($f);
[/code]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.