我需要在 Haskell 中解析一个 XML 文件,所以我选择了 HXT。到目前为止我喜欢它,但我无法弄清楚如何做一件事。
我正在解析的文件包含作为配置文件的信息。它的结构类似于
<clients>
<client>
<name>SomeName</name>
<info>MoreInfo</info>
<table>
<row>
<name>rowname1</name>
<value>rowvalue1</value>
</row>
<row>
<name>rowname2</name>
<value>rowvalue2</value>
</row>
</table>
</client>
...
</clients>
这种标记格式让我感到畏缩,但这是我必须使用的。
我在 Haskell 中对这些中的每一个都有如下记录
data Client = Client { name :: String, info :: String, table :: Table }
data Row = Row { name :: String, value :: String }
type Table = [Row]
我想从文件中获取数据作为Clients
. 我当前的代码看起来像
data Client = Client { name :: String, info :: String, table :: Table }
data Row = Row { name :: String, value :: String }
type Table = [Row]
getClients = atTag "client" >>>
proc client -> do
name <- childText "name" -< client
info <- childText "info" -< client
table <- getTable <<< atTag "table" -< client
returnA -< Client name info table
where
atTag tag = isElem >>> hasName tag
atChildTag tag = getChildren >>> atTag tag
text = getChildren >>> getText
childText tag = atChildTag tag >>> text
getTable = atChildTag "row" >>>
proc row -> do
name <- childText "name" -< row
value <- childText "value" -< row
returnA -< Row name value
但它不能编译,因为它只Row
从 中得到一个返回getTable
,而不是Row
s 的列表。由于这是我第一次使用 HXT,我知道我做错了什么,但我不知道如何修复它。
任何帮助都会很棒,谢谢!