xml-extractors: Extension to the xml package to extract data from parsed xml

[ bsd3, library, xml ] [ Propose Tags ]

This library provides functions to simplify extraction of data from generic xml tree structures (as produced by parsing xml with the xml package), while handling location information to provide decent error messages in case of errors.

Some limitations

  • Only handles unqualified names. (This is by design to simplify usage.)

  • No column number and sometimes no line number reference in error values.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.2.0.0, 0.2.1.0, 0.3.0.0, 0.4.0.0, 0.4.0.1, 0.4.0.2, 0.4.0.3
Change log changelog.md
Dependencies base (>=4.6 && <5), mtl (>=2.1 && <2.3), transformers (>=0.3 && <0.6), xml (>=1.3 && <1.4) [details]
License BSD-3-Clause
Author Johan Holmquist
Maintainer holmisen@gmail.com
Category XML
Home page https://github.com/holmisen/xml-extractors
Uploaded by holmisen at 2016-07-12T10:36:05Z
Distributions
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 3910 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for xml-extractors-0.4.0.2

[back to package description]

xml-extractors

This is an extension to the xml package, providing functions to extract data from parsed xml.

Motivation

The xml package provides functions to parse and get information from xml data. It will parse an xml string into a generic tree representation. Extracting information from such a tree to some custom data structure while keeping track of location to handle errors is tricky. This library helps with that.

If there is an error during extraction (expected information is absent or wrong), it will return an error value with position information. The idea is to provide decent error messages.

Example usage

Suppose you have an xml file of books like this:

<?xml version="1.0"?>
<library>
  <book id="1" isbn="23234-1">
    <author>John Doe</author>
    <title>Some book</title>
  </book>
  <book id="2">
    <author>You</author>
    <title>The Great Event</title>
  </book>
  ...
</library>

and a Haskell data type to represent a book:

data Book = Book { bookId        :: Int
                 , isbn          :: Maybe String
                 , author, title :: String
                 }

You can parse the xml file into a generic tree structure using parseXMLDoc from the xml package. Then to transform this generic xml tree into Book objects you define extractors for books, like so:

book = element "book" $ do
         i <- attribAs "id" integer
         s <- optional (attrib "isbn")
         children $ do
           a <- element "author" $ contents $ text
           t <- element "title" $ contents $ text
           return Book { bookId = i, author = a, title = t, isbn = s }

library = element "library" $ children $ only $ many book

extractLibrary :: Element -> Either ExtractionErr [Book]
extractLibrary = extractDocContents library