1

我试图将带有 HXT 的 XML '预解析'到 [XmlTree] 一次,然后多次重用这些数据。

下面是我的代码:

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import Text.XML.HXT.Core

parseXML = readDocument [ withValidate no
                        , withRemoveWS yes  -- throw away formating WS
                        ] 

atTag tag = deep (isElem >>> hasName tag)


data UiWindow = UiWindow {
    wndName :: String,
    wndNameLib :: String,
    wndChildren :: [UiWindow]
    } deriving (Show)

initUiWindow = UiWindow {
    wndName = "empty",
    wndNameLib = "",
    wndChildren = []
    }


parseDoc docName = runX $ parseXML fileName >>> getWindow
  where
    fileName = docName ++ ".xml"


getWindow = atTag "Window" >>> proc x -> do
    libraryItemName <- getAttrValue "libraryItemName" -< x
    name <- getAttrValue "name" -< x
    children <- arrIO parseDoc -< libraryItemName
    returnA -< initUiWindow { wndName = name, wndNameLib = libraryItemName, wndChildren = children}


documentName = "DOMDocument.xml"        

parseRoot = parseXML documentName

--runX (parseRoot >>> getWindow )

如果我事先解析:

λ: x <- runX parseRoot

λ: :t x
x :: [XmlTree]
λ: :t getWindow
getWindow :: IOSLA (XIOState ()) XmlTree UiWindow

我该如何运行这样的事情:

runX $ XYZ(x) >>> getWindow

或这个:

runX $ XYZ(x) >>> getSomethingElse

允许我重用“x”中的数据。

4

2 回答 2

1

您需要在 parseDoc 中执行与在 getWindow 中相同的操作:

runX $ parseXML filename >>> proc myDoc -> do
    window <- getWindow -< myDoc
    something <- getSomethingElse -< myDoc
于 2016-01-12T19:05:08.540 回答
1

在阅读了有关箭头的更多信息后,我发现了这一点:

λ: :t constA
constA :: ArrowList a => c -> a b c

现在constA在一个类型的元素上使用XmlTree我们得到一个可以重用的箭头:

main = do
  x <- runX parseRoot
  windows <- runX $ constA (head x) >>> getWindow
  doors <- runX $ constA (head x) >>> getDoor

我必须使用head xas xwill 的类型[XmlTree],但第二个箭头需要XmlTree

于 2016-01-16T21:25:58.297 回答