Yaml Usage Tips

Source: Internet
Author: User

In recent times, YAML has been touched, and as a serialized language, Yaml's readability seems to be easier to understand than XML, and more lightweight than CSV. At present, Yaml as a project automation framework of a data configuration file, in the use of the process by a certain praise ~ The following summarizes the experience in the actual use.

  The style of Yaml:

  

  How to use Yaml:

1 PrivateString Yamlfile;2 3 PrivateHashmap<string, Hashmap<string, string>>ml;4 5     /**6 * @function Constructors7     */8      PublicYamlutil () {9Yamlfile = "Test";Ten          This. Getyamlfile (); One     } A  -     /** - * @function loading Yaml files the     */ -@SuppressWarnings ("Unchecked") -      Public voidGetyamlfile () { -  +File f =NewFile ("test/" + Yamlfile + ". Yaml"); -         Try { +ml = Yaml.loadtype (NewFileInputStream (F.getabsolutepath ()), HashMap.class); A}Catch(FileNotFoundException e) { at e.printstacktrace (); -         } -     } -  -     /** - * @function Obtain the corresponding keyword information in the Yaml file in     * @paramkeyword -     * @return to     */ +      PublicMap<string,string>getkeywordinfos (String keyword) { -  thehashmap<string,string> map =NewHashmap<string,string>(); *Map =ml.get (keyword); $         returnmap;Panax Notoginseng     } -}

Yaml encounters a small pit:

  1. Note the full half of the colon .... It's not much to say.

2. Key value cannot be a full number

Test.yaml:

33033:  name:driver Age  :24

In this case, the result obtained when using ml.get (keyword) is null

3, the encoding format problem, if use Chinese please use UTF-8, cannot think Utf-8-bom format.

  

This style, when used in ml.get (keyword), also gets the same result as null.

    /*** @function get the corresponding keyword information in the yaml file *@paramkeyword *@return     */     PublicMap<string,string>getkeywordinfos (String keyword) {HashMap<String,String> map =NewHashmap<string,string>();  for(String key:ml.keySet ()) {System.out.println (Key.charat (0)); if(key.equals (keyword)) {map=ml.get (keyword); }        }                returnmap; } 

In the execution of key.equals (keyword), key is Father,keyword is also father but the actual result is false, further analysis found that key and keyword hashcode is not equal, Key.charat ( 0) The value is "", then a bit of a circle. Get the first 3 bytes of key discovery results as follows, which is a representation of UTF-8. Finally try to find is utf-8-bom in mischief.

Above is I in the use of the process encountered problems, in this, again thanks to fly the sum of children.

Yaml Usage Tips

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.