xml - 在 Arrow proc 上下文中提取多个元素

标签 xml haskell hxt

我想解析以下示例 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

当调用getChildrendo 之外-block,您甚至在进入 proc 之前就已经 promise 了一个 child 。 .里面proc ,你检查(例如)那个 child 是否有名字 Name和姓名 Sum .不出所料,您找不到任何符合这些矛盾要求的 child 。

通过移动 getChildren在里面,你允许遍历不同的 child (例如)ns .

关于xml - 在 Arrow proc 上下文中提取多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195952/

相关文章:

haskell - Haskell 中的符号

performance - Data.Vector.Unboxed.Mutable.MVector 的索引真的这么慢吗?

haskell - 如何正确收集Hxt程序的命令行选项?

haskell - 图库的 xml 树解析器 (Haskell)

python - 转义字符串以在 XML 中使用

c# - 在 C# 中读取一个简单的 XML 配置文件

haskell - GHCJS/Haste 可以自己编译吗?

xml - 使用HXT解析Haskell中的多个子节点

sql - 使用 SQL 查询读取 XML 子节点属性

java - MyBatis 中的 iBatis <isParameterPresent> 标签是什么?