我正在学习用它解析一个HXT
文件。一个例子是GPX。到目前为止,我得到了以下信息:
import Data.Time
import Text.XML.HXT.Core
data Gpx = Gpx [Trk] deriving (Show)
data Trk = Trk [TrkSeg] deriving (Show)
data TrkSeg = TrkSeg [TrkPt] deriving (Show)
data TrkPt = TrkPt Double Double deriving (Show)
parseGpx =
getChildren >>> isElem >>> hasName "gpx" >>>
getChildren >>> isElem >>> hasName "trk" >>>
parseGpxTrk >>> arr Gpx
parseGpxTrk = undefined
parseGpxTrkSegs = undefined
您可以看到它是不完整的,但它仍然应该输入check。不幸的是,我已经遇到了一个错误:
Couldn't match type ‘Trk’ with ‘[Trk]’
Expected type: Trk -> Gpx
Actual type: [Trk] -> Gpx
In the first argument of ‘arr’, namely ‘Gpx’
In the second argument of ‘(>>>)’, namely ‘arr Gpx’
这个错误表示的是,我试图将每个匹配项从
parseGpxTrk
箭头传递到arr Gpx
构造函数,但实际上我想要的是通过arr Gpx
构造函数传递整个匹配列表。那么,我如何得到
HXT
(或者通常是箭头?)将匹配项作为列表通过我的arr Gpx
构造函数而不是通过arr Gpx
构造函数传递列表中的每个条目? 最佳答案
我觉得这是个不错的解决方案
{-# LANGUAGE Arrows #-}
import Data.Maybe
import Text.Read
import Text.XML.HXT.Core
import Control.Applicative
data Gpx = Gpx [Trk] deriving (Show)
data Trk = Trk [TrkSeg] deriving (Show)
data TrkSeg = TrkSeg [TrkPt] deriving (Show)
data TrkPt = TrkPt Double Double deriving (Show)
最棘手的一个可能是
parseTrkPt
,因为为了正确地执行它,您必须处理从String
s到Double
的解析,这可能会失败。我已经决定让它返回一个Maybe TrkPt
,然后进一步处理这个问题:elemsNamed :: ArrowXml cat => String -> cat XmlTree XmlTree
elemsNamed name = isElem >>> hasName name
parseTrkPt :: ArrowXml cat => cat XmlTree (Maybe TrkPt)
parseTrkPt = elemsNamed "trkpt" >>>
proc trkpt -> do
lat <- getAttrValue "lat" -< trkpt
lon <- getAttrValue "lon" -< trkpt
returnA -< TrkPt <$> readMaybe lat <*> readMaybe lon
我在这里还使用了
proc
语法,因为我认为它会更清晰。TrkPt <$> readMaybe lat <*> readMaybe lon
的类型为Maybe TrkPt
,如果其中一个Nothing
s返回readMaybe
,则返回Nothing
。我们现在可以汇总所有成功的结果:parseTrkSeg :: (ArrowXml cat, ArrowList cat) => cat XmlTree TrkSeg
parseTrkSeg =
elemsNamed "trkseg" >>>
(getChildren >>> parseTrkPt >>. catMaybes) >. TrkSeg
括号在这里很重要,我花了一段时间才弄清楚那部分。根据放置paren的位置,您将得到不同的结果,例如
[TrkSeg [TrkPt a b], TrkSeg [TrkPt c d]]
而不是[TrkSeg [TrkPt a b, TrkPt c d]]
。紧随其后的解析器都遵循类似的模式:parseTrk :: ArrowXml cat => cat XmlTree Trk
parseTrk =
elemsNamed "trk" >>>
(getChildren >>> parseTrkSeg) >. Trk
parseGpx :: ArrowXml cat => cat XmlTree Gpx
parseGpx =
elemsNamed "gpx" >>>
(getChildren >>> parseTrk) >. Gpx
然后您可以非常简单地运行它,尽管您仍然需要钻取传递根元素:
main :: IO ()
main = do
gpxs <- runX $ readDocument [withRemoveWS yes] "ana.gpx"
>>> getChildren
>>> parseGpx
-- Pretty print the document
forM_ gpxs $ \(Gpx trks) -> do
putStrLn "GPX:"
forM_ trks $ \(Trk segs) -> do
putStrLn "\tTRK:"
forM_ segs $ \(TrkSeg pts) -> do
putStrLn "\t\tSEG:"
forM_ pts $ \pt -> do
putStr "\t\t\t"
print pt
诀窍是使用
ArrowList
类型类中的方法,尤其是>.
类型的方法。它聚合a b c -> ([c] -> d) -> a b d
中的元素,将其传递给一个将其转换为新类型的函数,然后在该新类型上输出一个新的ArrowList
。如果需要,甚至可以为最后3个解析器抽象一点:
nestedListParser :: ArrowXml cat => String -> cat XmlTree a -> ([a] -> b) -> cat XmlTree b
nestedListParser name subparser constructor
= elemsNamed name
>>> (getChildren >>> subparser)
>. constructor
parseTrkSeg :: (ArrowXml cat, ArrowList cat) => cat XmlTree TrkSeg
parseTrkSeg = nestedListParser "trkseg" (parseTrkPt >>. catMaybes) TrkSeg
parseTrk :: ArrowXml cat => cat XmlTree Trk
parseTrk = nestedListParser "trk" parseTrkSeg Trk
parseGpx :: ArrowXml cat => cat XmlTree Gpx
parseGpx = nestedListParser "gpx" parseTrk Gpx
如果您想完成gpx文件的其余语法,这可能很有用。