xml-tree parser (Haskell) for graph-library -


i'm writing library working graphs. primary task - parsing xml-tree. tree looks

<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 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 write data xml file structure? can not write parser xml tree of kind (hxt library)

assuming convert proper xml (surround attribute values quotes), following code 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) ->     nodes <- replicatem nodecount parsenode     arcs <- replicatem arccount parsearc     return $ graph nodes arcs       requirenum name =         x <- requireattr name         case reads $ unpack x of             (i, _):_ -> return             _ -> fail $ "invalid integer: " ++ unpack x     getcounts =         n <- requirenum "nodes"         <- 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]} 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -