Закончена работа над xmlParser, оттестировано

This commit is contained in:
Book Pauk
2020-12-08 18:48:55 +07:00
parent f3b69caa12
commit bfadf35c40
2 changed files with 37 additions and 14 deletions

View File

@@ -294,13 +294,13 @@ function getAttrsSync(tail, lowerCase = true) {
if (lowerCase)
name = name.toLowerCase();
if (name != '') {
const fullname = name;
const fn = name;
let ns = '';
if (fullname.indexOf(':') >= 0) {
[ns, name] = fullname.split(':');
if (fn.indexOf(':') >= 0) {
[ns, name] = fn.split(':');
}
result[name] = {value, ns, fullname};
result[name] = {value, ns, fn};
}
name = '';
value = '';

View File

@@ -1,8 +1,8 @@
const sax = require('../../sax');
const sax = require('./sax');
function formatXml(parsedXml, textFilterFunc) {
let out = '<?xml version="1.0" encoding="utf-8"?>';
out += formatXmlNode(parsedXml, textFilterFunc);
function formatXml(xmlParsed, encoding = 'utf-8', textFilterFunc) {
let out = `<?xml version="1.0" encoding="${encoding}"?>`;
out += formatXmlNode(xmlParsed, textFilterFunc);
return out;
}
@@ -68,6 +68,9 @@ function parseXml(xmlString, lowerCase = true) {
};
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
if (tag == '?xml')
return;
const newNode = {_n: tag, _p: node};
if (tail) {
@@ -77,7 +80,7 @@ function parseXml(xmlString, lowerCase = true) {
const attrs = {};
for (let i = 0; i < atKeys.length; i++) {
const attrName = atKeys[i];
attrs[parsedAttrs[attrName].fullname] = parsedAttrs[attrName].value;
attrs[parsedAttrs[attrName].fn] = parsedAttrs[attrName].value;
}
newNode._attrs = attrs;
@@ -105,16 +108,36 @@ function parseXml(xmlString, lowerCase = true) {
return result;
}
function simplifyXml(parsedXml) {
}
function simplifyXmlParsed(node) {
const simplifyNodeArray = (a) => {
const result = {};
function desimplifyXml(parsedXml) {
for (let i = 0; i < a.length; i++) {
const child = a[i];
if (child._n && !result[child._n]) {
result[child._n] = {};
if (child._a) {
result[child._n] = simplifyNodeArray(child._a);
}
if (child._t) {
result[child._n]._t = child._t;
}
if (child._attrs) {
result[child._n]._attrs = child._attrs;
}
}
}
return result;
};
return simplifyNodeArray([node]);
}
module.exports = {
formatXml,
formatXmlNode,
parseXml,
simplifyXml,
desimplifyXml
simplifyXmlParsed
}