0

I am parsing XML files that describe User Interface for a game and try to learn HXT at the same time. I can successfully parse a single XML file. But could not figure what would be the best way to open and parse other XML files whilst inside getWindow function.

Each XML consists of number of Windows. Each Window has name and libraryName. The latter being the name of the XML file that describes the window. For instance, the root looks like this:

<!-- DOMDocument.xml -->
<elements>
    <Window libraryItemName="window_home" name="window_home">
    <!-- data here  -->
    </Window>
    <Window libraryItemName="window_battle" name="window_battle">
    <!-- data here  -->
    </Window>
</elements>

And then there is a separate XML file for each window. E.g. "window_home":

<!-- window_home.xml -->
<elements>
    <Window libraryItemName="panel_tabs" name="panel_tabs" selected="true">
    <!-- data here  -->
    </Window>
    <Window libraryItemName="home_powerup_menu" name="home_powerup_menu" selected="true">
    <!-- data here  -->
    </Window>
    <Window libraryItemName="panel_name" name="panel_name" selected="true">
    <!-- data here  -->
    </Window>
</elements>

I parse root DOMDocument.xml with this code:

{-# 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 = []
    }


getWindow = atTag "Window" >>> 
  proc x -> do
    _libraryItemName <- getAttrValue "libraryItemName" -< x
    _name <- getAttrValue "name" -< x
    -- TODO: Open _libraryItemName XML file and parse windows in it
    returnA -< initUiWindow { wndName = _name, wndNameLib = _libraryItemName}

documentName = "DOMDocument.xml"        

parseRoot = parseXML documentName
--runX (parseRoot >>> getWindow )

Since the getWindow function is not wrapped inside IO what would be the best way to achieve a desired behaviour?

4

1 回答 1

1

HXT 组合子是多态的,并且有一种类型IOLA可以实现所有与 XML 解析相关的类型类,此外ArrowIO还可以实现IO中箭头。

例如,如果你想对文件进行完全递归解析,你可以做一些简单的事情

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}
于 2015-12-22T14:15:32.790 回答