框和XML格式模块 [英] Boxes and XML in Format module

查看:70
本文介绍了框和XML格式模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用格式化模块. 提示格式如下:

I would like to print an output using boxes and XML in Format module. An idea format is as follows:

<Events>
   <Event>
      <name> haha </name>
      <name> haha </name>
   </Event>
   <Event>
      <name> lili </name>
      <name> lili </name>
   </Event>
   <Event>
      <name> lolo </name>
      <name> lolo </name>
   </Event>
</Events>

目前我的代码如下,它不能完全打印出我期望的样子(我在这里省略了不正确的结果).

At the moment my code is as follows, it does not print exactly what I expect (I omit to put the incorrect result here).

  (* in event.ml *)
  let print_name (fmt: formatter) (x: t) : unit =
    Format.open_tag "Name";
    Format.fprintf fmt "%s" (get_name x);
    Format.close_tag ()

  (* in events.ml *)
  let print (fmt: formatter) (x: t) : unit =
    let print (fmt: formatter) (x: t) : unit =
      List.iter
      (fun m ->
         Format.open_tag "Event";
         Format.fprintf fmt "@,@[<v 4>%a@,%a@," Event.print_name m 
                                     Event.print_name m; (* print twice *)
         Format.close_tag ();
         Format.fprintf fmt "@,@]")
      x
    in
    Format.open_tag "Events";
    Format.fprintf fmt "@,@[<v 4>%a@]@," print x;
    Format.close_tag ()

    (* in main.ml *)
    Format.fprintf Format.std_formatter "%a" Events.print x

我不确定我对这个框是否很好理解,尤其是在涉及XML的情况下.有谁知道如何正确编写这些格式?

I am not sure I understand well the box, especially when XML is involved. Does anyone know how to write these formats correctly?

推荐答案

Format对于带有缩进的数据打印很有用,但是其缩进算法有些棘手,并且很难完美地模拟其他缩进策略.也许自己处理缩进级别会更容易.

Format is useful to print data with indentation, but its indentation algorithm is a bit tricky and is hard to simulate other indentation policies perfectly. Maybe it is easier to handle indentation levels by yourself.

这篇关于框和XML格式模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆