Работа над XmlParser

This commit is contained in:
Book Pauk
2022-11-09 14:44:00 +07:00
parent 40f72d17e6
commit 04a8ba8426

View File

@@ -455,6 +455,8 @@ class XmlParser extends NodeBase {
throw new Error('JSON parse error: root element must be array'); throw new Error('JSON parse error: root element must be array');
this.rawNodes = parsed; this.rawNodes = parsed;
return this;
} }
toString(options = {}) { toString(options = {}) {
@@ -641,9 +643,15 @@ class XmlParser extends NodeBase {
}); });
this.rawNodes = parsed; this.rawNodes = parsed;
return this;
} }
toObject() { toObject(options = {}) {
const {
compactText = false
} = options;
const nodesToObject = (nodes) => { const nodesToObject = (nodes) => {
const result = {}; const result = {};
@@ -663,8 +671,9 @@ class XmlParser extends NodeBase {
if (node.value) { if (node.value) {
Object.assign(newNode, nodesToObject(node.value)); Object.assign(newNode, nodesToObject(node.value));
//схлопывание узла до строки //схлопывание текстового узла до string
if (!Array.isArray(newNode) if (compactText
&& !Array.isArray(newNode)
&& Object.prototype.hasOwnProperty.call(newNode, '*TEXT') && Object.prototype.hasOwnProperty.call(newNode, '*TEXT')
&& Object.keys(newNode).length === 1) { && Object.keys(newNode).length === 1) {
newNode = newNode['*TEXT']; newNode = newNode['*TEXT'];
@@ -736,6 +745,8 @@ class XmlParser extends NodeBase {
}; };
this.rawNodes = objectToNodes(xmlObject); this.rawNodes = objectToNodes(xmlObject);
return this;
} }
} }