Search code examples
haskelldata-structureshaskell-lenslenses

Haskell | How to obtain a value from a deeply nested data structure?


I am a beginner in haskell and I'm trying to learn more about the language by doing some basic parsing.

I have some code that parses an XML file and produces this

[ Element
    { elName = QName
        { qName = "title"
        , qURI = Nothing
        , qPrefix = Nothing
        }
    , elAttribs = []
    , elContent =
        [ Text
            ( CData
                { cdVerbatim = CDataText
                , cdData = "This string is what I want to obtain" -- string to view.
                , cdLine = Just 27
                }
            )
        ]
    , elLine = Just 27
    }
]

Where Element is just a XML library's data type

What I want to do is get the string "This string is what I want to obtain"

I'm not sure how to do it without unwrapping the entire data structure. I found that way really messy and error prone.

I did some general research and found the Lens library, and although there were some tutorials, I'm still struggling to parse a nested data structure.

This is the XML file I'm trying to parse looks

<GoodreadsResponse>
    <Request>
        <authentication>true</authentication>
        <key>HOKCk4yYS8UjyducqmgRw</key>
        <method>search_search</method>
    </Request>
    <search>
        <query>fantasy</query>
        <results-start>1</results-start>
        <results-end>20</results-end>
        <total-results>35221</total-results>
        <source>Goodreads</source>
        <query-time-seconds>0.21</query-time-seconds>
        <results>
            <work>
                <id type="integer">2384</id>
                <books_count type="integer">51</books_count>
                <ratings_count type="integer">78825</ratings_count>
                <text_reviews_count type="integer">3357</text_reviews_count>
                <original_publication_year type="integer">2002</original_publication_year>
                <original_publication_month type="integer">2</original_publication_month>
                <original_publication_day type="integer">18</original_publication_day>
                <average_rating>4.17</average_rating>
                <best_book type="Book">
                    <id type="integer">84136</id>
                    <title>Fantasy Lover (Hunter Legends Series #1)</title>
                    <author>
                        <id type="integer">4430</id>
                        <name>Sherrilyn Kenyon</name>
                    </author>
                    <image_url>https://images.gr-assets.com/books/1348332807m/84136.jpg</image_url>
                    <small_image_url>https://images.gr-assets.com/books/1348332807s/84136.jpg</small_image_url>
                </best_book>
            </work>
            <work>
                <id type="integer">6734901</id>
                <books_count type="integer">42</books_count>
                <ratings_count type="integer">18358</ratings_count>
                <text_reviews_count type="integer">985</text_reviews_count>
                <original_publication_year type="integer">2010</original_publication_year>
                <original_publication_month type="integer" nil="true"/>
                <original_publication_day type="integer" nil="true"/>
                <average_rating>4.26</average_rating>
                <best_book type="Book">
                    <id type="integer">6542645</id>
                    <title>Fantasy in Death (In Death, #30)</title>
                    <author>
                        <id type="integer">17065</id>
                        <name>J.D. Robb</name>
                    </author>
                    <image_url>https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png</image_url>
                    <small_image_url>https://s.gr-assets.com/assets/nophoto/book/50x75-a91bf249278a81aabab721ef782c4a74.png</small_image_url>
                </best_book>
            </work>
       ...
       ...

Solution

  • Using list comprehensions and record accessors is fairly clear:

    get :: [Element] -> [String]
    get es = [cdData c | e <- es, Text c <- elContent e ]
    

    The Text c pattern will automatically filter out any Elem e or CRef s values in elContent e.

    Once you learn that, for lists, =<< means concatMap, you can save a few characters with

    get :: [Element] -> [String]
    get es = [cdData c | Text c <- elContent =<< es]
    

    Additionaly, if you only wanted the cdData when the cdVerbatim was CDataText, you can add that condition.

    get :: [Element] -> [String]
    get es = [cdData c | Text c <- elContent =<< es, cdVerbatim c == CDataText ]