用于图库的xml树分析器(Haskell) [英] xml-tree parser (Haskell) for graph-library

查看:114
本文介绍了用于图库的xml树分析器(Haskell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写图库来处理图形。
主要任务 - 解析xml树。
该树看起来像

 < graph nodes = 4 arcs = 5> 
< node id = 1 />
< node id = 2 />
< node id = 3 />
< node id = 4 />
< arc from = 1 to = 2 />
< arc from = 1 to = 3 />
< arc from = 1 to = 4 />
< arc from = 2 to = 4 />
< arc from = 3 to = 4 />
< /图表>

存储结构:

  type Id = Int 

data Node =节点Id派生(显示)
数据Arc =弧Id Id派生(显示)

data Graph = Graph {nodes :: [Node],
arcs :: [Arc]}

如何将xml文件中的数据写入此结构?
我无法为这种类型的xml树(HXT库)编写解析器。假设您将这个解析器转换为转换为正确的XML(用引号包围所有的属性值),下面的代码可以工作(使用xml-enumerator):

  { - #LANGUAGE OverloadedStrings# - } 
import Text.XML.Enumerator.Parse
import Control.Monad
import Data.Text(unpack)
import Control.Applicative

类型Id = Int

数据节点=节点Id派生(显示)
数据Arc =弧Id Id导出(显示)

数据图=图{nodes:[Node],
arcs :: [Arc]}
导出显示

main = parseFile_graph.xmldecodeEntities $ force图形需要parseGraph

parseGraph = tagNamegraphgetCounts $ \(nodeCount,arcCount) - > do
节点< - replicateM nodeCount parseNode
arcs< - replicateM arcCount parseArc
return $图形节点arcs
其中
requireNum name = do
x< ; - requireAttr name
case reads $ $ unpack x of
(i,_):_ - >返回i
_ - >失败$无效整数:++解包x
getCounts = do
n< - requireNum节点
a< - requireNum弧形
return(n,a)
parseNode = forcenode required$ tagNamenode
(Node< $> requireNumid)return
parseArc = forcearc required$ tagNamearc
(Arc< $> requireNumfrom<> requireNum到)返回

输出:

 图形{nodes = [节点1,节点2,节点3,节点4],弧形= [弧1 2,弧1 3,弧1 4,弧2 4,弧3 4]} 


I'm writing a library for working with graphs. The primary task - parsing xml-tree. The tree looks like

<graph nodes=4 arcs=5>
    <node id=1 />
    <node id=2 />
    <node id=3 />
    <node id=4 />
    <arc from=1 to=2 />
    <arc from=1 to=3 />
    <arc from=1 to=4 />
    <arc from=2 to=4 />
    <arc from=3 to=4 />
</graph>

Structure for storing:

type Id = Int

data Node = Node Id deriving (Show)
data Arc = Arc Id Id deriving (Show)

data Graph = Graph { nodes :: [Node],
             arcs  :: [Arc]}

How to write data from the xml file into this structure? I can not write a parser for xml tree of this kind (HXT library)

解决方案

Assuming that you convert that into proper XML (surround all the attribute values with quotes), the following code will work (using xml-enumerator):

{-# LANGUAGE OverloadedStrings #-}
import Text.XML.Enumerator.Parse
import Control.Monad
import Data.Text (unpack)
import Control.Applicative

type Id = Int

data Node = Node Id deriving (Show)
data Arc = Arc Id Id deriving (Show)

data Graph = Graph { nodes :: [Node],
             arcs  :: [Arc]}
  deriving Show

main = parseFile_ "graph.xml" decodeEntities $ force "graph required" parseGraph

parseGraph = tagName "graph" getCounts $ \(nodeCount, arcCount) -> do
    nodes <- replicateM nodeCount parseNode
    arcs <- replicateM arcCount parseArc
    return $ Graph nodes arcs
  where
    requireNum name = do
        x <- requireAttr name
        case reads $ unpack x of
            (i, _):_ -> return i
            _ -> fail $ "Invalid integer: " ++ unpack x
    getCounts = do
        n <- requireNum "nodes"
        a <- requireNum "arcs"
        return (n, a)
    parseNode = force "node required" $ tagName "node"
        (Node <$> requireNum "id") return
    parseArc = force "arc required" $ tagName "arc"
        (Arc <$> requireNum "from" <*> requireNum "to") return

Outputs:

Graph {nodes = [Node 1,Node 2,Node 3,Node 4], arcs = [Arc 1 2,Arc 1 3,Arc 1 4,Arc 2 4,Arc 3 4]}

这篇关于用于图库的xml树分析器(Haskell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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