gctGenerator/docs/.vuepress/components/scripts/parseXml.js

22 lines
655 B
JavaScript
Raw Normal View History

2020-06-28 06:33:20 +09:00
const parseXml = (xmlString) => {
const codeCollection = new DOMParser()
.parseFromString(xmlString, "text/xml")
.getElementsByTagName("code");
const codes = [...codeCollection];
return codes.map((code) => ({
author: parseTextNode(code, "author"),
title: parseTextNode(code, "title"),
description: parseTextNode(code, "description"),
version: parseTextNode(code, "version"),
date: parseTextNode(code, "date"),
source: parseTextNode(code, "source").replace(/[\s\n\r\t]+/gm, ""),
}));
};
const parseTextNode = (node, identifier) =>
node.getElementsByTagName(identifier)[0].textContent;
export default parseXml;