我试图使用HXT从6行的
XML输入中提取一些数据.我也希望保留HXT,因为Curl集成,因为我有其他XML文件有数千行,稍后.
我的XML看起来像这样:
<?xml version = "1.0" encoding = "UTF-8"?> <find> <set_number>228461</set_number> <no_records>000000008</no_records> <no_entries>000000008</no_entries> </find>
我一直试图聚在一起解析它.不幸的是,HXT的Wiki页面并没有太大的帮助(或者我只是忽略了一些东西).
data FindResult = FindResult { resultSetNumber :: String,resultnorecords :: Int,resultNoEntries :: Int } deriving (Eq,Show) resultParser :: ArrowXml a => a XmlTree FindResult resultParser = hasName "find" >>> getChildren >>> proc x -> do 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) where query = "http://" ++ server ++ "/find?request=" ++ str
我一直得到的是
*** Exception: Prelude.head: empty list
解决方法
以下适合我(以
this example为模型):
{-# LANGUAGE Arrows #-} module Main where import Text.XML.HXT.Core import System.Environment data FindResult = FindResult { resultSetNumber :: String,Show) resultParser :: ArrowXml a => a XmlTree FindResult resultParser = deep (isElem >>> hasName "find") >>> proc x -> do 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 = do [src] <- getArgs res <- runX $( readDocument [withValidate no] src >>> resultParser) print . head $res
测试:
$dist/build/test/test INPUT FindResult {resultSetNumber = "228461",resultnorecords = 8,resultNoEntries = 8}