The hexml-lens package

[ Tags: bsd3, lens, library ] [ Propose Tags ]

Lenses for the hexml package


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.1, 0.2.0, 0.2.1
Dependencies base (>=4.7 && <5), bytestring, contravariant, foundation, hexml, lens, profunctors, text [details]
License BSD3
Copyright All Rights Reserved
Author Jose Iborra
Maintainer pepeiborra@gmail.com
Category lens
Home page https://github.com/pepeiborra/hexml-lens#readme
Uploaded Fri Aug 18 08:57:21 UTC 2017 by PepeIborra
Distributions NixOS:0.2.1, Stackage:0.2.1
Downloads 162 total (19 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-08-18 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for hexml-lens-0.2.1

[back to package description]

hexml-lens

Hackage Stackage Nightly Travis Build Status

Folds and getters for the Hexml Node type.

{-# LANGUAGE RecordWildCards #-}
import Control.Lens hiding (children)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as LB
import Network.Wreq
import Text.XML.Hexml
import Text.XML.Hexml.Lens

url :: [Char]
url = "http://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/data/courses/reed.xml"

data Place s = Place { building :: s , room :: Int}
  deriving Show

data Course s = Course { title, instructor :: s , place :: Place s}
  deriving Show

main :: IO ()
main = do
  r <- get url
  let stripDocType = LB.unlines . drop 2 . LB.lines
  let courses = take 10 $ r ^.. responseBody . to stripDocType . _XML . _children . courseF
  print courses

courseF :: Fold Node (Course B.ByteString)
courseF = runFold $ do
  title <- Fold $ node "title" . textContents
  instructor <- Fold $ node "instructor" . textContents
  place <- Fold $ node "place" . placeF
  return $ Course{..}

placeF :: Fold Node (Place B.ByteString)
placeF = runFold $ do
  building <- Fold $ node "building" . textContents
  room     <- Fold $ node "room" . textContents . _Show
  return (Place building room)