Last active
June 18, 2017 19:54
-
-
Save freshyill/3dfb15393adf7f664fefd870f378042c to your computer and use it in GitHub Desktop.
Convert JATS XML to JSON with Camaro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const transform = require("camaro"); | |
const dedent = require("deline"); | |
const xmlDir = "test_xml"; | |
fs.readdir(xmlDir, (err, files) => { | |
files.forEach(file => { | |
const xml = fs.readFileSync(xmlDir + "/" + file, "utf-8", (err, data) => { | |
if (err) throw err; | |
}); | |
const cleanXml = dedent(xml); | |
// \(\bfig?\b\.[\s\S][^\)]+(\)) | |
// Maybe we'll strip figure references later. | |
const template = { | |
title: "article/front/article-meta/title-group/article-title", | |
journal: "article/front/journal-meta/journal-title-group/journal-title", | |
doi: "article/front/article-meta/article-id[@pub-id-type='doi']", | |
author: ["article/front/article-meta/contrib-group/contrib[@contrib-type='author']/name", { | |
given_name: "given-names", | |
surname: "surname" | |
}], | |
body: "article/body", | |
volume: "article/front/article-meta/volume", | |
issue: "article/front/article-meta/issue", | |
date: "article/front/article-meta/pub-date", | |
fpage: "article/front/article-meta/fpage", | |
lpage: "article/front/article-meta/lpage", | |
}; | |
const result = transform(cleanXml, template); | |
const jsonResult = JSON.stringify(result); | |
const jsonFileName = file.replace(".xml",".json"); | |
console.log(jsonResult); | |
fs.writeFile("issueJson/" + jsonFileName, jsonResult, (err) => { | |
if (err) { | |
if (err) throw err; | |
} else { | |
console.log("Created " + jsonFileName); | |
} | |
}) | |
}); | |
}); | |
// \(\bfig?\b\.[\s\S][^\)]+(\)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment