我想解析以下示例XML文件,而没有pickler模块。
<?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>
Run Code Online (Sandbox Code Playgroud)
我结束了这个:
{-# 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
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用。如果我尝试在proc上下文中仅提取单个元素,那么它将起作用。但是尝试提取多个元素将始终失败\返回空列表。我可能对构图有误解>>>。
我用这个例子 runLa (xreadDoc >>> readGroups)