我如何在HXT中输出XMLTrees? [英] How do i output XMLTrees in HXT?

查看:103
本文介绍了我如何在HXT中输出XMLTrees?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从xml文件中提取标签,并根据属性将每个标签写入单独的文件中。

I am trying to extract tags from a xml file and write each one to a seperate file based on an attribute.

提取部分并不那么难:

The extraction part isn't that hard:

*Main> ifs <- runX ( readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "TagName" >>> getAttrValue "Name" &&& this)
*Main> :t ifs
ifs :: [(String, XmlTree)]

我试图映射writeDocument在第二个条目上,但没有成功。我明白我必须以某种方式将它带回IO Monad ......但是不知道如何实现这一点。

I tried to map writeDocument over the second entries but had no sucess. I understand that i have to get it back into the IO Monad somehow ... but have no idea on how to achieve this.

出于测试的目的,我已经从结果中提取了这些XmlTree:

for testing purposes i have extracted on of those XmlTrees from the result:

*Main> let x = (map snd ifs) !! 0
*Main> :t x
x :: XmlTree

我可以在 x上运行箭头像这样:

*Main> runLA (getName) x
["TagName"]

但是当我尝试写它时到一个文件,我收到错误信息,指出我不在IO Monade(我认为):

But when i try to write it to a file i get error message indicating that i am not in the IO Monade (i think):

*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 没有帮助:

Changing runLA to runIOSLA doesn't help:

*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






这就是我可以得到的。


That's as far as i can get.

正如Travis Brown指出的那样可以在一个箭头中做到这一点:

As Travis Brown points out it is possible to do this in one arrow:

runX . applyA $ readDocument [withCurl [],withExpat yes] "file.xml" >>> getElement "Tag" >>> getAttrValue "DEF" &&& this >>> arr (\ (n,x) -> root [] [constA x] >>> writeDocument [] n)


推荐答案

使用 root constA 制作一个文档根节点箭头出来:

Use root and constA to make an document root arrow out of your node:

runX (root [] [constA x] >>> writeDocument [] "test.xml")

这应该按预期工作。

这篇关于我如何在HXT中输出XMLTrees?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆