View Category
Process an XML document
Given the XML Document:
<shopping>
<item name=
<item name=
</shopping>
Print out the total cost of the items, e.g. $14.50
<shopping>
<item name=
"bread" quantity="3" price="2.50"/>
<item name=
"milk" quantity="2" price="3.50"/>
</shopping>
Print out the total cost of the items, e.g. $14.50
haskell
File Edit Options Buffers Tools Haskell Help
import Text.ParserCombinators.Parsec
import Control.Monad
import System ( getArgs )
data Item = Item { name :: String,
quantity :: Int,
price :: Int }
deriving Show
type Basket = [Item]
item :: Parser Item
item = do string "<item name=\""
name <- manyTill letter (char '\"')
string " quantity=\""
quantity <- liftM read $ many digit
string "\" price=\""
dollars <- liftM read $ many digit
cents <- option 0 (char '.' >> (liftM read $ many digit))
string "\"/>"
return $ Item name quantity (100 * dollars + cents)
basket :: Parser Basket
basket = do string "<shopping>"
items <- manyTill item (try $ string "</shopping>")
return items
parseBasket :: String -> Basket
parseBasket input = case parse basket "Shopping Basket" input of
Left _ -> []
Right val -> val
main = do args <- getArgs
putStrLn $ show . (/100) . fromIntegral . sum . map (\(Item _ q p) -> q * p) . parseBasket $ head args
import Text.ParserCombinators.Parsec
import Control.Monad
import System ( getArgs )
data Item = Item { name :: String,
quantity :: Int,
price :: Int }
deriving Show
type Basket = [Item]
item :: Parser Item
item = do string "<item name=\""
name <- manyTill letter (char '\"')
string " quantity=\""
quantity <- liftM read $ many digit
string "\" price=\""
dollars <- liftM read $ many digit
cents <- option 0 (char '.' >> (liftM read $ many digit))
string "\"/>"
return $ Item name quantity (100 * dollars + cents)
basket :: Parser Basket
basket = do string "<shopping>"
items <- manyTill item (try $ string "</shopping>")
return items
parseBasket :: String -> Basket
parseBasket input = case parse basket "Shopping Basket" input of
Left _ -> []
Right val -> val
main = do args <- getArgs
putStrLn $ show . (/100) . fromIntegral . sum . map (\(Item _ q p) -> q * p) . parseBasket $ head args
import Text.XML.HXT.Core
import Text.Printf
main :: IO ()
main = do
prices <- runX (process "basket.xml")
printf "$%.2f\n" $ sum prices
process :: FilePath -> IOSArrow XmlTree Double
process filename =
readDocument [withValidate no] filename >>>
getChildren >>>
isElem >>> hasName "shopping" >>>
getChildren >>>
isElem >>> hasName "item" >>>
getQuantity &&& getPrice >>>
arr (uncurry (*))
getQuantity :: IOSArrow XmlTree Double
getQuantity =
getAttrl >>> hasName "quantity" >>> getChildren >>>
getText >>> arr read
getPrice :: IOSArrow XmlTree Double
getPrice =
getAttrl >>> hasName "price" >>> getChildren >>>
getText >>> arr read
import Text.Printf
main :: IO ()
main = do
prices <- runX (process "basket.xml")
printf "$%.2f\n" $ sum prices
process :: FilePath -> IOSArrow XmlTree Double
process filename =
readDocument [withValidate no] filename >>>
getChildren >>>
isElem >>> hasName "shopping" >>>
getChildren >>>
isElem >>> hasName "item" >>>
getQuantity &&& getPrice >>>
arr (uncurry (*))
getQuantity :: IOSArrow XmlTree Double
getQuantity =
getAttrl >>> hasName "quantity" >>> getChildren >>>
getText >>> arr read
getPrice :: IOSArrow XmlTree Double
getPrice =
getAttrl >>> hasName "price" >>> getChildren >>>
getText >>> arr read
create some XML programmatically
Given the following CSV:
bread,3,2.50
milk,2,3.50
Produce the equivalent information in XML, e.g.:
<shopping>
<item name=
<item name=
</shopping>
bread,3,2.50
milk,2,3.50
Produce the equivalent information in XML, e.g.:
<shopping>
<item name=
"bread" quantity="3" price="2.50" />
<item name=
"milk" quantity="2" price="3.50" />
</shopping>
