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.
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]
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
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!
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!