I want to parse the following sample XML file, without the pickler module.
<?xml version="1.0" encoding="utf-8" ?>
<Groups>
<Name>ABC</Name>
<GroupA>
<Name>Foo</Name>
<Sum>100</Sum>
</GroupA>
<GroupB>
<Name>Bar</Name>
<Sum>0</Sum>
</GroupB>
</Groups>
I ended up with this:
{-# language Arrows #-}
import Text.XML.HXT.Core
data Groups = Groups GroupA GroupB deriving Show
data GroupA = GroupA String String deriving Show
data GroupB = GroupB String String deriving Show
readGroup :: LA XmlTree Groups
readGroup = deep (isElem >>> hasName "Groups") >>> getChildren >>>
proc root -> do
a <- readGroupA -< root
b <- readGroupB -< root
returnA -< Groups a b
readGroupA :: LA XmlTree GroupA
readGroupA = isElem >>> hasName "GroupA" >>> getChildren >>>
proc root -> do
n <- isElem >>> hasName "Name" /> getText -< root
s <- isElem >>> hasName "Sum" /> getText -< root
returnA -< GroupA n s
readGroupB :: LA XmlTree GroupB
readGroupB = isElem >>> hasName "GroupB" >>> getChildren >>>
proc root -> do
n <- isElem >>> hasName "Name" /> getText -< root
s <- isElem >>> hasName "Sum" /> getText -< root
returnA -< GroupB n s
Unfortunately, this does not work.
If I try to extract just a single element in a proc context it works.
But trying to extract multiple elements will always fail\ return the empty list. I might have a misunderstanding of the composition >>>.
I run the example with runLa (xreadDoc >>> readGroups)
Try this:
When the call to
getChildrenis outside thedo-block, you are committing to one child before even entering theproc. Inside theproc, you check (for instance) whether that child has nameNameand nameSum. Unsurprisingly, you don't find any child fitting these contradictory requirements.By moving
getChildreninside, you allow different children to be traversed for (for instance)nands.