Login | Register   
LinkedIn
Google+
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
 

Beyond XML and JSON: YAML for Java Developers : Page 2

Get a high-level introduction to the YAML file format for Java developers. Find out how it differs from XML and JSON and examine its relative advantages and drawbacks.


advertisement
Elements of YAML Structure
Now that you have seen a quick sample of YAML, let's delve deeper into the typical elements of YAML structure: hashes, lists, and block literals.

Hashes
You create hashes by indenting the children and separating the key from the value with a colon (:), as follows:

JFrame: defaultCloseOperation: JFrame.EXIT_ON_CLOSE title: Test Frame width: 800 height: 400



Alternatively, you can create hashes by using the JSON-compatible braces syntax ({}), where each key/value is comma-separated:

JFrame: {defaultCloseOperation: JFrame.EXIT_ON_CLOSE, title: Test, Frame, width: 800, height: 400}

Lists
You create lists by prefixing each element of the list with a minus sign (-), combined with whitespace indentation—the cornerstone of YAML:

components: - JTextArea - JButton

Alternatively, you can create lists by using the JSON-compatible brackets syntax ([]), for example:

components: [JTextArea, JButton]

Block Literals
This is where YAML really shines, in particular compared with XML and its ugly CDATA hack. Block literals make inserting large blocks of text into a file trivially easy. You can preserve the newlines in your text by using the vertical line (|) directive as follows:

text: | This is a really long text that spans multiple lines (but preserves new lines). It does not need to be escaped with special brackets, CDATA tags, or anything like that

The YAML processor will start the text from the first character in the first line (and discard all leading whitespace used for indentation), but preserves all the newlines in your text.

Alternatively, you can use the greater-than (>) directive to tell the YAML processor to strip all newlines and treat the entered text as one long line of text:

text: > This is a really long text that spans multiple lines (but preserves new lines). It does not need to be escaped with special brackets, CDATA tags, or anything like that

Besides these two directives, you also can use vertical line and plus sign (|+), which strips leading whitespace and preserves newlines and trailing whitespace, and greater-than and minus sign (>-), which strips all whitespace.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap