图库的 xml 树解析器 (Haskell)

发布于 2024-10-31 17:22:18 字数 622 浏览 1 评论 0原文

我正在编写一个用于处理图形的库。 主要任务 - 解析 xml-tree。 该树看起来像用于

<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>

存储的结构:

type Id = Int

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

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

如何将xml文件中的数据写入该结构中? 我无法为这种类型的 xml 树编写解析器(HXT 库)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-11-07 17:22:18

您需要使用 XML 库吗? 'tagsoup' 库对于 not-really-xml 可能同样有效,如下所示:

import Text.HTML.TagSoup
import Data.Maybe

main = do
    s <- readFile "A.dat"

    -- get a list of nodes and arcs
    let g' = catMaybes
                [ case n of
                    TagOpen "node" [(_,n)]        -> Just (Left  $ Node (read n)) 
                    TagOpen "arc"  [(_,n), (_,m)] -> Just (Right $ Arc (read n) (read m))
                    _ -> Nothing

                | n <- parseTags s ]

    -- collapse them into a graph
    let g = foldr (\n g -> case n of
                                Left  n -> g { nodes = n : nodes g }
                                Right a -> g { arcs  = a : arcs  g }
                        ) (Graph [] []) g'

    print g

运行此:

> main
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]}

Do you need to use an XML library? The 'tagsoup' library might be just as effective for not-really-xml like this:

import Text.HTML.TagSoup
import Data.Maybe

main = do
    s <- readFile "A.dat"

    -- get a list of nodes and arcs
    let g' = catMaybes
                [ case n of
                    TagOpen "node" [(_,n)]        -> Just (Left  $ Node (read n)) 
                    TagOpen "arc"  [(_,n), (_,m)] -> Just (Right $ Arc (read n) (read m))
                    _ -> Nothing

                | n <- parseTags s ]

    -- collapse them into a graph
    let g = foldr (\n g -> case n of
                                Left  n -> g { nodes = n : nodes g }
                                Right a -> g { arcs  = a : arcs  g }
                        ) (Graph [] []) g'

    print g

Running this:

> main
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]}
黑凤梨 2024-11-07 17:22:18

假设您将其转换为正确的 XML(用引号将所有属性值括起来),则以下代码将起作用(使用 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 <
gt; requireNum "id") return
    parseArc = force "arc required" $ tagName "arc"
        (Arc <
gt; requireNum "from" <*> requireNum "to") return

输出:

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]}

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 <
gt; requireNum "id") return
    parseArc = force "arc required" $ tagName "arc"
        (Arc <
gt; 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]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文