UNPKG

@openade/fe

Version:

Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)

75 lines 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XmlService = void 0; const fast_xml_parser_1 = require("fast-xml-parser"); class XmlService { constructor() { this.parser = new fast_xml_parser_1.XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_', textNodeName: '#text', parseAttributeValue: true, trimValues: true, }); this.builder = new fast_xml_parser_1.XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: '@_', textNodeName: '#text', format: true, indentBy: ' ', suppressEmptyNode: false, suppressBooleanAttributes: false, }); } async parse(xml) { try { return this.parser.parse(xml); } catch (error) { throw new Error(`XML parsing failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async stringify(obj, _options) { try { return this.builder.build(obj); } catch (error) { throw new Error(`XML stringification failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async validate(xml, _schema) { const errors = []; try { this.parser.parse(xml); return { valid: errors.length === 0, errors }; } catch (error) { errors.push(error instanceof Error ? error.message : 'Unknown validation error'); return { valid: false, errors }; } } async extractElements(xml, xpath) { try { const parsed = await this.parse(xml); return this.extractByPath(parsed, xpath); } catch (error) { throw new Error(`Element extraction failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } extractByPath(obj, path) { const parts = path.split('.'); let current = obj; for (const part of parts) { if (current && typeof current === 'object') { current = current[part]; } else { return []; } } return Array.isArray(current) ? current : [current]; } } exports.XmlService = XmlService; //# sourceMappingURL=xml.service.js.map