我想解析以下示例 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>
我最终得到了这个:
{-# 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
不幸的是,这不起作用。
如果我尝试仅提取
proc
中的单个元素它工作的上下文。但是尝试提取多个元素总是会失败\返回空列表。我可能对作文有误解
>>>
.我使用
runLa (xreadDoc >>> readGroups)
运行示例
最佳答案
试试这个:
readGroup :: LA XmlTree Groups
readGroup = deep (isElem >>> hasName "Groups") >>>
proc root -> do
a <- getChildren >>> readGroupA -< root
b <- getChildren >>> readGroupB -< root
returnA -< Groups a b
readGroupA :: LA XmlTree GroupA
readGroupA = isElem >>> hasName "GroupA" >>>
proc root -> do
n <- getChildren >>> isElem >>> hasName "Name" /> getText -< root
s <- getChildren >>> isElem >>> hasName "Sum" /> getText -< root
returnA -< GroupA n s
readGroupB :: LA XmlTree GroupB
readGroupB = isElem >>> hasName "GroupB" >>>
proc root -> do
n <- getChildren >>> isElem >>> hasName "Name" /> getText -< root
s <- getChildren >>> isElem >>> hasName "Sum" /> getText -< root
returnA -< GroupB n s
当调用
getChildren
在 do
之外-block,您甚至在进入 proc
之前就已经 promise 了一个 child 。 .里面proc
,你检查(例如)那个 child 是否有名字 Name
和姓名 Sum
.不出所料,您找不到任何符合这些矛盾要求的 child 。通过移动
getChildren
在里面,你允许遍历不同的 child (例如)n
和 s
.
关于xml - 在 Arrow proc 上下文中提取多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195952/