我正在尝试从xml文件中提取标签,并根据属性将每个标签写入单独的文件。
提取部分并不那么难:
*Main> ifs <- runX ( readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "TagName" >>> getAttrValue "Name" &&& this)
*Main> :t ifs
ifs :: [(String, XmlTree)]
我试图将writeDocument映射到第二个条目但没有成功。我知道我必须以某种方式将它重新带回IO Monad ......但不知道如何实现这一目标。
出于测试目的,我从结果中提取了那些XmlTrees:
*Main> let x = (map snd ifs) !! 0
*Main> :t x
x :: XmlTree
我可以像x
这样运行箭头:
*Main> runLA (getName) x
["TagName"]
但是当我尝试将其写入文件时,我收到错误消息,表明我不在IO Monade中(我认为):
*Main> runLA (writeDocument [] "test.xml") x
<interactive>:1:8:
Couldn't match expected type `LA a0 b0'
with actual type `IOSLA (XIOState s0) XmlTree XmlTree'
Expected type: LA a0 b0
Actual type: IOStateArrow s0 XmlTree XmlTree
In the return type of a call of `writeDocument'
In the first argument of `runLA', namely
`(writeDocument [] "test.xml")'
将runLA
更改为runIOSLA
无效:
*Main> runIOSLA (writeDocument [] "test.xml") x
<interactive>:1:40:
Couldn't match expected type `XIOState s0'
with actual type `Data.Tree.NTree.TypeDefs.NTree XNode'
Expected type: XIOState s0
Actual type: XmlTree
In the second argument of `runIOSLA', namely `x'
In the expression: runIOSLA (writeDocument [] "test.xml") x
这就是我能得到的。
正如特拉维斯·布朗所指出的,可以用一个箭头来做到这一点:
runX . applyA $ readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "Tag" >>> getAttrValue "DEF" &&& this >>> arr (\ (n,x) -> root [] [constA x] >>> writeDocument [] n)