UNPKG

isoxml

Version:

JavaScript library to parse and generate ISOXML (ISO11783-10) files

35 lines (34 loc) 1.15 kB
import { XMLBuilder, XMLParser } from 'fast-xml-parser'; const XML_PARSE_OPTIONS = { textNodeName: '_text', attributeNamePrefix: '', ignoreAttributes: false, attributesGroupName: '_attributes', parseTagValue: false, parseAttributeValue: false, isArray: (tagName, jPath, isLeafNode, isAttribute) => !isAttribute, attributeValueProcessor: (name, value) => value.replace(/&amp;/g, '&') .replace(/&apos;/g, "'") .replace(/&quot;/g, '"') .replace(/&lt;/g, '<') .replace(/&gt;/g, '>') }; const XML_BUILD_OPTIONS = { textNodeName: '_text', attributeNamePrefix: '', ignoreAttributes: false, attributesGroupName: '_attributes', format: true, attributeValueProcessor: (name, value) => value.replace(/&/g, '&amp;') .replace(/'/g, '&apos;') .replace(/"/g, '&quot;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;') }; export function js2xml(json) { return `<?xml version="1.0" encoding="UTF-8"?> ${new XMLBuilder(XML_BUILD_OPTIONS).build(json)}`; } export function xml2js(xml) { return new XMLParser(XML_PARSE_OPTIONS).parse(xml); }