使用HXT在Haskell中解析多个子节点 [英] Parsing multiple child nodes in Haskell with HXT
问题描述
我需要在Haskell中解析XML文件,因此我选择了HXT.到目前为止,我还是很喜欢的,但是我很难弄清楚该怎么做.
I needed to parse an XML file in Haskell, so I chose HXT. I like it so far, but I'm having trouble figuring out how to do one thing.
我正在解析的文件包含信息作为配置文件.它的结构类似于
The file I'm parsing contains information as a config file. It has a structure similar to
<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>
这种标记格式使我感到畏缩,但这是我必须使用的.
This markup format makes me cringe, but it's what I have to work with.
在Haskell中,我分别记录了以下内容
I have records for each of these in Haskell as follows
data Client = Client { name :: String, info :: String, table :: Table }
data Row = Row { name :: String, value :: String }
type Table = [Row]
我想从文件中获取数据作为Clients
的列表.我当前的代码看起来像
And I want to get the data out of the file as a list of Clients
. My current code looks like
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,所以我知道我做错了事,但是我不知道如何解决它.
But it doesn't compile because it only gets a single Row
back from getTable
, instead of a list of Row
s. Since this is my first time using HXT, I know I'm doing something wrong, but I don't know how to fix it.
任何帮助都会很棒,谢谢!
Any help would be great, thanks!
推荐答案
我最终找到了一个相关问题的答案,我不知道listA
的存在(我对Arrows还是很新的) ,并解决了问题!
I ended up finding the answer in a related problem, I didn't know of the existence of listA
(I'm also really new to Arrows), and that fixed it!
这篇关于使用HXT在Haskell中解析多个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!