Question

I would like to parse an .mxl file (MusicXML) with clojure

So far i've seen a lot of tools to work with .xml files but I can't find a way to work with .mxl, maybe i should convert mxl to xml first but i don't know how to do that neither.

Was it helpful?

Solution

From the Wikipedia page on MusicXML:

The textual representation listed above is verbose; MusicXML v2.0 addresses this by adding a compressed zip format with a .mxl suffix that can make files roughly one-twentieth the size of the uncompressed version.[16]

I'm guessing that your .mxl file is an XML file which has been compressed, and this is why you're getting parse errors. As far I can gather, the compression algorithm is a zip algorithm, so you should be able to use java's zip functionality to get at it.

EDIT

I just had a go at this with a sample .mxl file I found online. The .mxl file, once unzipped contained the xml file within it. I was then able to use following (inspired by this answer) to get the raw XML...

 (defn extract-mxl [path]
   (let [[_ filename] (re-matches #"(.*)\.mxl$" (.getName (java.io.File. path)))
         zipfile (java.util.zip.ZipFile. path)
         zipentry (.getEntry zipfile (str filename ".xml"))
         in (.getInputStream zipfile zipentry)] 
     (slurp in)))

OTHER TIPS

MusicXML's zip structure for .mxl files is similar to that of the EPUB format. See http://www.musicxml.com/tutorial/compressed-mxl-files/zip-archive-structure/ in the MusicXML tutorial for more information about where to find the XML score data within the MXL archive.

if someone stumbles upon this, and can't figure out how to unzip the compressed mxl file:

on mac, I just renamed it to .zip and it worked out fine!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top