haskell - Getting Data from a Simple XML -


i trying extract data xml input 6 lines, using hxt. want keep hxt, too, because of curl integration , because have other xml files thousands of lines, later.

my xml looks this:

<?xml version = "1.0" encoding = "utf-8"?> <find>     <set_number>228461</set_number>     <no_records>000000008</no_records>     <no_entries>000000008</no_entries> </find> 

and i've been trying how parse that. unfortunately, wiki page of hxt has not been big (or did overlook stuff).

data findresult = findresult {         resultsetnumber :: string,         resultnorecords :: int,         resultnoentries :: int     } deriving (eq, show)  resultparser :: arrowxml => xmltree findresult resultparser = hasname "find" >>> getchildren >>> proc x ->     setnumber <- iselem >>> hasname "set_number" >>> getchildren >>> gettext -< x     norecords <- iselem >>> hasname "no_records" >>> getchildren >>> gettext -< x     noentries <- iselem >>> hasname "no_entries" >>> getchildren >>> gettext -< x     returna -< findresult setnumber (read norecords) (read noentries)  find str = return . head =<< (runx $ readdocument [withvalidate no, withcurl []] query >>> resultparser)     query = "http://" ++ server ++ "/find?request=" ++ str 

what is

*** exception: prelude.head: empty list 

so, guess, parsing must go horribly wrong, since checked , correctly xml query.

the following works me (modelled after this example):

{-# language arrows #-}  module main         import text.xml.hxt.core import system.environment  data findresult = findresult {         resultsetnumber :: string,         resultnorecords :: int,         resultnoentries :: int     } deriving (eq, show)  resultparser :: arrowxml => xmltree findresult resultparser =   deep (iselem >>> hasname "find") >>> proc x ->     setnumber <- gettext <<< getchildren <<< deep (hasname "set_number") -< x     norecords <- gettext <<< getchildren <<< deep (hasname "no_records") -< x     noentries <- gettext <<< getchildren <<< deep (hasname "no_entries") -< x     returna -< findresult setnumber (read norecords) (read noentries)  main :: io () main = [src] <- getargs           res <- runx $ ( readdocument [withvalidate no] src >>> resultparser)           print . head $ res 

testing:

$ dist/build/test/test input findresult {resultsetnumber = "228461", resultnorecords = 8, resultnoentries = 8} 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -