xml - 在 Arrow proc 上下文中提取多个元素
问题描述
我想解析以下示例 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
当调用在-blockgetChildren
之外时,您甚至在进入. 在 内部,您检查(例如)该孩子是否有 name和name 。不出所料,您找不到任何符合这些矛盾要求的孩子。do
proc
proc
Name
Sum
通过getChildren
向内移动,您允许遍历(例如)n
和的不同子级s
。
推荐阅读
- java - 实例化时返回 null 的静态内部类
- sas - 获取 BP 之后的最后一个 FT 的 SAS 代码
- php - 数据未通过 PHP 完全从 MySQL 导出到 CSV
- alexa - Alexa - 在 python 中实现 CanFulfillIntentRequest
- python - ClipByValue 不存在于 Shap 包的 tf_ops._gradient_registry._registry
- json - 动态过滤python字典中的信息
- c# - “TextBox”不包含“ID”的定义
- java - java.sql.SQLException: 找不到合适的驱动程序 (SQL Server Express)
- python - 从活动的第一天开始检测用户流失
- python - Scrapy:如何为不同的项目使用不同的 settings.py