Search code examples
haskellrsshxt

If tag misses when parsing an RSS file in Haskell with HXT


I have to parse an RSS file in Haskell and I do something like:

atTag tag = deep (isElem >>> hasName tag)
getRSSDetails = atTag "channel" >>>
   proc p -> do 
      fTitle         <- gotoAndTake "title"        -< p
      fLink          <- gotoAndTake "link"         -< p
      fDescription   <- gotoAndTake "description"  -< p
      fLanguage      <- gotoAndTake "language"     -< p
      fGenerator     <- gotoAndTake "generator"    -< p
      fCopyright     <- gotoAndTake "copyright"    -< p
      fWebMaster     <- gotoAndTake "webMaster"    -< p
      fLastBuildDate <- gotoAndTake "lastBuildDate" -< p
                where gotoAndTake a = (getChildren >>> 
                          isElem >>> 
                          hasName a >>> 
                          getChildren >>> 
                          getText)

My problem is that when one tag is missing, let's say "lastBuildDate" from the RSS file than I get an empty list, but I want just to replace that item with "".

How can I do that ?? Thanks,

main = do
   rssFeeds <- runX (parseRSS "rss.xml" >>> getRSSDetails)
   print rssFeeds

EDIT1: Solved by adding orElse (constA "") at the end of where gotoAndTake a ...


Solution

  • Solved by adding

    orElse (constA "") 
    

    at the end of

    where gotoAndTake a = (getChildren >>> 
                       isElem >>> 
                       hasName a >>> 
                       getChildren >>> 
                       getText)