Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using YAML to Decrease Data Transfer Bandwidth Requirements : Page 4

This new alternative to XML ain't markup language, but for data serialization, it serves the same purpose, albeit with greater brevity!


advertisement
What the PHP Script Does
You must separate each line in the YAML document by a carriage return and line feed (\r\n). To generate this output, your PHP simply queries the database (an expanded service would parameterize this for specific stock tickers and date ranges) for the data using a the query 'Select * from Prices'. It then does the following:

  • Output '—-' indicating the start of the document.
  • For each record in the resultset:
  • Output 'Day: ' followed by the value of the date in the current row. The date needs to be contained within inverted commas (\"), and the line has to end with a "\r\n"
  • Output the string "values:" followed by a \r\n.
  • For each of the data columns (open, close, high and low) output the descriptive string (i.e. "open: "), followed by the value, followed by a \r\n.
Running the query will return a page that looks similar to Figure 2.
 
Figure 2. Running the YAML Generator: When you run the YAML Generator, the resulting page doesn't display the line breaks in the generated YAML.
The results in Figure 2 may not look too good—but don't worry, it's just that the browser doesn't handle the "\r\n" too well. Save the file to a text file called yamlgen.yaml, and then open it with WordPad, and it will look much more like what you'd expect.

Parsing the Data
The Java YAML parser doesn't currently explode the data into a usable set of data structures; instead, it simply outputs the values to the Java console as it scans through the data, though it does collect them properly within their intended structures. You can see the output of the parser in Listing 1. Next Steps
There are many useful scenarios for simplified data formats such as YAML—one example might be to retrieve data from a database on the server side and chart it with a client. If you don't want to use XML, then YAML is a good solution, and the YAML Java parser gives you a head start in parsing the retrieved data. It's not a complete solution yet, as it doesn't allow you to load data from a URL, and it doesn't load the data into a DOM that you can work with. You'll have to take the data from the various elements as sliced out by the parser, and instead of outputting them to the console (as in Listing 1), do something useful with them (such as charting).



The biggest challenge facing YAML is the lack of good parser support in the .NET and Java platforms. YAML is already well supported in Ruby, Python, and Caml, and has emerging support in PHP. Still, if you need a serialization schema to pass data between systems and don't want to start from scratch, you could do a whole lot worse than YAML!



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap