Board logo

標題: 將某個自定義既data轉成某種format用咩做? [打印本頁]

作者: 3ldk    時間: 2020-5-6 12:37     標題: 將某個自定義既data轉成某種format用咩做?

例如我有個咁既yaml
  1. cat:
  2. - name: cat A
  3.   color: white
  4. - name: cat B
  5.   color: Black

  6. dog:
  7. - name: dog A
  8.   color: white
  9. - name: dog B
  10.   color: Black
複製代碼
想轉成html比report用
  1. <table>
  2.     <tr>
  3.         <th>name</th><th>color</th>
  4.     </tr>
  5.     <tr>
  6.         <td>cat A</td><td>white</td>
  7.     </tr>
  8.     <tr>
  9.         <td>cat B</td><td>Black</td>
  10.     </tr>
  11. </table>

  12. <table>
  13.     <tr>
  14.         <th>name</th><th>color</th>
  15.     </tr>
  16.     <tr>
  17.         <td>dog A</td><td>white</td>
  18.     </tr>
  19.     <tr>
  20.         <td>dog B</td><td>Black</td>
  21.     </tr>
  22. </table>
複製代碼
甚至轉成class比program用
  1. class Cat {
  2.   public $name;
  3.   public $color;
  4.   function get_name() {
  5.     return $this->name;
  6.   }
  7.   function get_color() {
  8.     return $this->color;
  9.   }
  10. }

  11. class Dog {
  12.   public $name;
  13.   public $color;
  14.   function get_name() {
  15.     return $this->name;
  16.   }
  17.   function get_color() {
  18.     return $this->color;
  19.   }
  20. }
複製代碼
咁樣可以點做? 難道要自己寫program read個data, 然後砌成一個string再write file?
咁咪要花好多工夫而且容易出錯? 先唔講其他, 你要output一個formatted的html/class都要花唔少人工去做.
會唔會有比較簡單既做法? 會唔會係用DSL去做? 以前一直搞唔清楚DSL究竟係做乜, 直到有以上需要, 感覺好似係用黎做依D野.
如果以上無估錯的話, 具體係點做?
作者: FizzyDrink    時間: 2020-5-6 21:09

https://en.wikipedia.org/wiki/XML_transformation_language
this works for you?
作者: KamSing    時間: 2020-5-7 01:32

Parser Generator?
BTW, code formatter都好common...





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2