Skip to content

Instantly share code, notes, and snippets.

@freshyill
Last active June 18, 2017 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freshyill/3dfb15393adf7f664fefd870f378042c to your computer and use it in GitHub Desktop.
Save freshyill/3dfb15393adf7f664fefd870f378042c to your computer and use it in GitHub Desktop.
Convert JATS XML to JSON with Camaro
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