Skip to content

Instantly share code, notes, and snippets.

@adbrowne
Created February 21, 2012 11:56
Show Gist options
  • Save adbrowne/1876099 to your computer and use it in GitHub Desktop.
Save adbrowne/1876099 to your computer and use it in GitHub Desktop.
Haskell HaXml Sample of Lazily Reading XML file (in this case badges file from stackoverflow data dump)
import Text.XML.HaXml.ParseLazy as PL
import Text.XML.HaXml
import Text.XML.HaXml.Posn
import System.IO
main = do
inh <- openFile "badges.xml" ReadMode
hSetEncoding inh utf8_bom
inpStr <- hGetContents inh
let (Document _ _ root _) = PL.xmlParse "badges.xml" inpStr
let rootElem = CElem root noPos
print $ length $ tag "badges" rootElem
let rows = tag "badges" /> tag "row" $ rootElem
let first = head rows
let (CElem (Elem name attributes _)_) = first
print name
print attributes
hClose inh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment