gpt4 book ai didi

xml - 使用HXT解析Haskell中的多个子节点

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:10 26 4
gpt4 key购买 nike

我需要在 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

但它无法编译,因为它只从 getTable 返回一个 Row,而不是 Row 的列表。由于这是我第一次使用 HXT,我知道我做错了什么,但我不知道如何解决。

任何帮助都会很棒,谢谢!

最佳答案

我最终在一个相关问题中找到了答案,我不知道 listA 的存在(我也是 Arrows 的新手),然后就解决了!

关于xml - 使用HXT解析Haskell中的多个子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com