Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6095699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:50:29+00:00 2026-05-23T12:50:29+00:00

I am trying to extract some data from an XML input with 6 lines,

  • 0

I am trying to extract some data from an XML input with 6 lines, using HXT. I want to keep HXT, too, because of the Curl integration and because I have other XML files with thousands of lines, later.

My XML looks like this:

<?xml version = "1.0" encoding = "UTF-8"?>
<find>
    <set_number>228461</set_number>
    <no_records>000000008</no_records>
    <no_entries>000000008</no_entries>
</find>

And I’ve been trying to get together how to parse that. Unfortunately, the Wiki page of HXT has not been a big help (or I just did overlook stuff).

data FindResult = FindResult {
        resultSetNumber :: String,
        resultNoRecords :: Int,
        resultNoEntries :: Int
    } deriving (Eq, Show)

resultParser :: ArrowXml a => a XmlTree FindResult
resultParser = hasName "find" >>> getChildren >>> proc x -> do
    setNumber <- isElem >>> hasName "set_number" >>> getChildren >>> getText -< x
    noRecords <- isElem >>> hasName "no_records" >>> getChildren >>> getText -< x
    noEntries <- isElem >>> hasName "no_entries" >>> getChildren >>> getText -< x
    returnA -< FindResult setNumber (read noRecords) (read noEntries)

find str = return . head =<< (runX $ readDocument [withValidate no, withCurl []] query >>> resultParser)
    where query = "http://" ++ server ++ "/find?request=" ++ str

What I always get is

*** Exception: Prelude.head: empty list

so, I guess, the parsing must go horribly wrong, since I checked and correctly get the XML from the query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:50:30+00:00Added an answer on May 23, 2026 at 12:50 pm

    The following works for me (modelled after this example):

    {-# LANGUAGE Arrows #-}
    
    module Main
           where
    
    import Text.XML.HXT.Core
    import System.Environment
    
    data FindResult = FindResult {
            resultSetNumber :: String,
            resultNoRecords :: Int,
            resultNoEntries :: Int
        } deriving (Eq, Show)
    
    resultParser :: ArrowXml a => a XmlTree FindResult
    resultParser =
      deep (isElem >>> hasName "find") >>> proc x -> do
        setNumber <- getText <<< getChildren <<< deep (hasName "set_number") -< x
        noRecords <- getText <<< getChildren <<< deep (hasName "no_records") -< x
        noEntries <- getText <<< getChildren <<< deep (hasName "no_entries") -< x
        returnA -< FindResult setNumber (read noRecords) (read noEntries)
    
    main :: IO ()
    main = do [src] <- getArgs
              res <- runX $ ( readDocument [withValidate no] src >>> resultParser)
              print . head $ res
    

    Testing:

    $ dist/build/test/test INPUT
    FindResult {resultSetNumber = "228461", resultNoRecords = 8, resultNoEntries = 8}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a code to extract some form data from a HTML
I am trying to extract some meta data from Oracle about foreign keys. I
I'm trying to programmatically extract some data from this url: http://www.treasurydirect.gov/NP/BPDLogin?application=np The issue is
I'm trying to use ruby and mongoid in order to extract some data from
I am trying to retrieve the text data from an ePub file using Java.
Im trying to extract a line from wget's result but having trouble with it.
I am trying to extract a table of values from an excel (2003) spreadsheet
I'm trying to extract all matches from a EBML definition, which is something like
I'm trying to extract the domain name from a string in C#. You don't
I'm trying to parse a html page and extract 2 values from a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.