【问题标题】:How can I lazily parse big XHTML file in Clojure?如何在 Clojure 中懒惰地解析大 XHTML 文件?
【发布时间】:2012-12-29 07:25:57
【问题描述】:

我有一张大表的有效 XHTML 文件(100 兆字节的数据)。第一个 tr 是列(用于数据库),所有其他 tr 都是数据。它是整个文档中唯一的表,结构为 html->body->div->table。

如何在 Clojure 中以惰性方式解析它?

我知道data.xml,但因为我是 Clj 初学者,所以很难让它发挥作用。特别是因为 REPL 在处理这么大的文件时非常慢。

【问题讨论】:

标签: parsing xhtml clojure html-parsing


【解决方案1】:

data.xml docs 说它创建了一个文档的惰性树:parse。我在本地查了一下,好像是真的:

; Load libs
(require '[clojure.data.xml :as xml])
(require '[clojure.java.io :as io])

; standard.xml is 100MB xml file from here http://www.xml-benchmark.org/downloads.html
(def xml-tree (xml/parse (io/reader "standard.xml")))
(:tag xml-tree) => :site

(def child (first (:content xml-tree)))
(:tag child) => :regions

(dorun (:content xml-tree)) => REPL hangs for ~30 seconds on my computer because it tries to parse whole file

【讨论】:

  • 使用 xml/parse 的结果的一种惯用方式是使用 zip/xml-zip 以及 data.zip 中定义的特定于 XML 的 zip 过滤器 - 谷歌“Clojure xml zip”对于一些过时的教程。
猜你喜欢
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 2018-08-20
相关资源
最近更新 更多