@rxap/xml-parser
Version:
Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into
33 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestingXmlParserService = void 0;
const reflect_metadata_1 = require("@rxap/reflect-metadata");
const metadata_keys_1 = require("../decorators/metadata-keys");
const element_1 = require("../element");
const xml_parser_service_1 = require("../xml-parser.service");
class TestingXmlParserService extends xml_parser_service_1.XmlParserService {
parseFromXmlTesting(xml, elementParser) {
this.register(elementParser);
const elementName = (0, reflect_metadata_1.getMetadata)(metadata_keys_1.ElementParserMetaData.NAME, elementParser);
let xmlDoc;
try {
xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');
}
catch (e) {
throw new Error('Could not parse xml string');
}
if (!xmlDoc.childNodes.length) {
throw new Error('The parsed xml has not any element');
}
const root = new element_1.RxapElement(xmlDoc.childNodes.item(0), this.DOMParser, this.elementOptions);
if (root.nodeName.toLowerCase() === 'parsererror') {
throw new Error(root.getTextContent());
}
if (!elementName || !root.hasName(elementName)) {
throw new Error(`The root node must be an <${elementName}> element. Found a <${root.name}>`);
}
return this.parse(root, root.name, null, []);
}
}
exports.TestingXmlParserService = TestingXmlParserService;
//# sourceMappingURL=testing-xml-parser-service.js.map