Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
26 lines (24 sloc) 1011 Bytes
{-# LANGUAGE OverloadedStrings, TupleSections #-}
import System.Environment (getArgs)
import System.IO (stdin, stdout, openFile, IOMode(..), hFlush)
import System.Exit (exitSuccess)
import Control.Applicative
import Data.Conduit
import qualified Data.Conduit.Binary as C
import qualified Data.Conduit.List as C
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Aeson (encode)
import Text.XML.ToJSON (tokensToJSON)
import Text.HTML.TagStream.Text (tokenStreamBS)
main :: IO ()
main = do
args <- getArgs
(hInput, hOutput) <- case args of
[] -> return (stdin, stdout)
[input] -> (,stdout) <$> openFile input ReadMode
[input, output] -> (,) <$> openFile input ReadMode
<*> openFile output WriteMode
_ -> putStrLn "xml2json [input] [output]" >> exitSuccess
tokens <- runResourceT $ C.sourceHandle hInput $= tokenStreamBS $$ C.consume
L.hPutStrLn hOutput $ encode $ tokensToJSON tokens
hFlush hOutput