Loading XML to Haskell Converter...
Please wait a moment

How to Convert XML to Haskell Data Types

Step 1

Paste or Upload Your XML

Paste XML into the left editor, upload an .xml or .txt file, or press Sample to load a worked example.

Example XML

<?xml version="1.0" encoding="UTF-8"?>
<academic-paper id="PAPER-2024-001" status="published">
  <metadata>
    <title>Functional Programming in Haskell: A Comprehensive Study</title>
    <publication-date>2024-03-15</publication-date>
    <doi>10.1000/haskell.2024.001</doi>
    <is-peer-reviewed>true</is-peer-reviewed>
  </metadata>
  <journal>
    <name>Journal of Functional Programming</name>
    <volume>34</volume>
    <issue>2</issue>
    <impact-factor>2.85</impact-factor>
  </journal>
</academic-paper>
Step 2

Let It Read the Structure

The Haskell output updates as you type. Every element and attribute is looked at, including all copies of repeated elements. Broken XML shows the line and column of the first problem instead of code.

•
Records: one data type per element with children or attributes, using record syntax and Maybe fields
•
XML reading: a parse function per record built on xml-conduit cursors
•
JSON: Generic-based ToJSON and FromJSON instances, so Aeson can write the same values out as JSON
•
Types: Int, Int64, Integer, Double, Bool or Text, chosen from every value in your document
Step 3

Check the Generated Records

Look over the record types, field names and types. The original XML names stay in the parse functions, so the Haskell names can follow Haskell conventions.

Haskell output for the example

The module also starts with the language pragmas and imports, has a Journal record after these two, and ends with the reading helpers:

data AcademicPaper = AcademicPaper
  { academicPaperId :: Maybe Text -- attribute id
  , academicPaperStatus :: Maybe Text -- attribute status
  , academicPaperMetadata :: Maybe Metadata
  , academicPaperJournal :: Maybe Journal
  }
  deriving (Show, Eq, Generic)

instance ToJSON AcademicPaper
instance FromJSON AcademicPaper

-- Reads one <academic-paper> element.
parseAcademicPaper :: C.Cursor -> AcademicPaper
parseAcademicPaper cur = AcademicPaper
  { academicPaperId = xmlAttr "id" Nothing cur
  , academicPaperStatus = xmlAttr "status" Nothing cur
  , academicPaperMetadata = listToMaybe (map parseMetadata (xmlChildren "metadata" Nothing cur))
  , academicPaperJournal = listToMaybe (map parseJournal (xmlChildren "journal" Nothing cur))
  }

data Metadata = Metadata
  { metadataTitle :: Maybe Text
  , metadataPublicationDate :: Maybe Text
  , metadataDoi :: Maybe Text
  , metadataIsPeerReviewed :: Maybe Bool
  }
  deriving (Show, Eq, Generic)

instance ToJSON Metadata
instance FromJSON Metadata

-- Reads one <metadata> element.
parseMetadata :: C.Cursor -> Metadata
parseMetadata cur = Metadata
  { metadataTitle = listToMaybe (mapMaybe (readText . xmlText) (xmlChildren "title" Nothing cur))
  , metadataPublicationDate = listToMaybe (mapMaybe (readText . xmlText) (xmlChildren "publication-date" Nothing cur))
  , metadataDoi = listToMaybe (mapMaybe (readText . xmlText) (xmlChildren "doi" Nothing cur))
  , metadataIsPeerReviewed = listToMaybe (mapMaybe (readBool . xmlText) (xmlChildren "is-peer-reviewed" Nothing cur))
  }
Step 4

Copy or Download the Module

Copy the code or download it as XmlData.hs, add xml-conduit, text and aeson to your Cabal or Stack project, and read documents with the parse function of the root element.

•
Copy to clipboard and paste into your project
•
Download XmlData.hs and rename the module if your project uses a different one
•
Use the JSON instances with Aeson or a Servant API, or drop them if you only need XML

What is XML to Haskell Conversion?

It means writing Haskell types that match the shape of an XML document, so the rest of your program works with typed records instead of walking cursors by hand. Doing that by hand gets tedious once a document has a few levels of nesting and some repeated elements.

This converter looks at the whole document, merges every copy of each element, and writes a record for each element that has children or attributes. Each record gets a parse function that reads the document with xml-conduit, plus Aeson instances in case you want to pass the data on as JSON.

Frequently Asked Questions

What does the generated Haskell module contain?

One record type for each XML element that has child elements or attributes, each deriving Show, Eq and Generic, with empty ToJSON and FromJSON instances so Aeson can turn it into JSON and back. Every record also gets a parse function, such as parseAuthor, that reads that element with xml-conduit. A few small reading helpers sit at the bottom of the module.

How are the field types chosen?

From the text of every value the element has in your document. Whole numbers that fit 32 bits become Int, bigger ones up to 64 bits become Int64 (so an ID like 8901240544102066246 keeps every digit), and anything longer becomes Integer. Numbers with a decimal point become Double, and only the exact words true and false become Bool. Values with leading zeros such as 00501, hex like 0x1F and elements that are sometimes empty stay Text, because reading them as numbers would change or drop data.

How do I read an XML document with these types?

Add xml-conduit, text and aeson to your build-depends, then load the file with Text.XML.readFile def and pass C.fromDocument doc to the parse function of the root element, for example parseAcademicPaper. Child elements are found by local name and namespace, attributes are read directly, and numbers and booleans are converted with Data.Text.Read. A value that does not read as its type is left out (Nothing, or missing from the list) instead of crashing. The output was checked with GHC 9.4, xml-conduit 1.9 and aeson 2.1.

Why do the field names start with the type name?

Haskell record fields share one namespace across the whole module, so two records with a name field would not compile. Prefixing each field with its record, as in authorName and journalName, keeps every field unique without needing the DuplicateRecordFields extension. The JSON keys Aeson writes use these same field names.

What happens with repeated elements and different copies of the same element?

All copies of an element name are merged before any code is written. A field that only appears in the second <author> still makes it into the record, and if a value is a number in one copy and text in another, the field becomes Text. An element that appears more than once under the same parent becomes a list such as [Author]; one that appears once becomes a Maybe.

Is my XML uploaded anywhere?

No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server.