所以我需要阅读一个大型XML文件。我不想将它解析为DOM树并将其全部保存在内存中。我正在寻找的是一个能够在Haskell中提供对XML数据的快速,非缓存,仅向前访问的读者。
编辑:在同一主题上似乎有一些问题,但我没有在那里找到答案。所以我想要的是:
编辑: Text.XML.Enumerator.Parse似乎是the answer: 这是一个不错的选择吗?
答案 0 :(得分:2)
我用于类似任务的最佳模块是来自Text.XML.Stream.Parse包的xml-conduit。模块索引包含API的这个简单示例:
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.Trans.Resource
import Data.Conduit (($$))
import Data.Text (Text, unpack)
import Text.XML.Stream.Parse
data Person = Person Int Text
deriving Show
parsePerson = tagName "person" (requireAttr "age") $ \age -> do
name <- content
return $ Person (read $ unpack age) name
parsePeople = tagNoAttr "people" $ many parsePerson
main = do
people <- runResourceT $
parseFile def "people.xml" $$ force "people required" parsePeople
print people