update, text, response
This commit is contained in:
43
node_modules/fast-xml-parser/src/xmlbuilder/json2xml.js
generated
vendored
43
node_modules/fast-xml-parser/src/xmlbuilder/json2xml.js
generated
vendored
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
//parse Empty Node as self closing node
|
||||
const buildFromOrderedJs = require('./orderedJs2Xml');
|
||||
const getIgnoreAttributesFn = require('../ignoreAttributes')
|
||||
|
||||
const defaultOptions = {
|
||||
attributeNamePrefix: '@_',
|
||||
@@ -38,11 +39,12 @@ const defaultOptions = {
|
||||
|
||||
function Builder(options) {
|
||||
this.options = Object.assign({}, defaultOptions, options);
|
||||
if (this.options.ignoreAttributes || this.options.attributesGroupName) {
|
||||
if (this.options.ignoreAttributes === true || this.options.attributesGroupName) {
|
||||
this.isAttribute = function(/*a*/) {
|
||||
return false;
|
||||
};
|
||||
} else {
|
||||
this.ignoreAttributesFn = getIgnoreAttributesFn(this.options.ignoreAttributes)
|
||||
this.attrPrefixLen = this.options.attributeNamePrefix.length;
|
||||
this.isAttribute = isAttribute;
|
||||
}
|
||||
@@ -71,14 +73,16 @@ Builder.prototype.build = function(jObj) {
|
||||
[this.options.arrayNodeName] : jObj
|
||||
}
|
||||
}
|
||||
return this.j2x(jObj, 0).val;
|
||||
return this.j2x(jObj, 0, []).val;
|
||||
}
|
||||
};
|
||||
|
||||
Builder.prototype.j2x = function(jObj, level) {
|
||||
Builder.prototype.j2x = function(jObj, level, ajPath) {
|
||||
let attrStr = '';
|
||||
let val = '';
|
||||
const jPath = ajPath.join('.')
|
||||
for (let key in jObj) {
|
||||
if(!Object.prototype.hasOwnProperty.call(jObj, key)) continue;
|
||||
if (typeof jObj[key] === 'undefined') {
|
||||
// supress undefined node only if it is not an attribute
|
||||
if (this.isAttribute(key)) {
|
||||
@@ -88,6 +92,8 @@ Builder.prototype.j2x = function(jObj, level) {
|
||||
// null attribute should be ignored by the attribute list, but should not cause the tag closing
|
||||
if (this.isAttribute(key)) {
|
||||
val += '';
|
||||
} else if (key === this.options.cdataPropName) {
|
||||
val += '';
|
||||
} else if (key[0] === '?') {
|
||||
val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
|
||||
} else {
|
||||
@@ -99,9 +105,9 @@ Builder.prototype.j2x = function(jObj, level) {
|
||||
} else if (typeof jObj[key] !== 'object') {
|
||||
//premitive type
|
||||
const attr = this.isAttribute(key);
|
||||
if (attr) {
|
||||
if (attr && !this.ignoreAttributesFn(attr, jPath)) {
|
||||
attrStr += this.buildAttrPairStr(attr, '' + jObj[key]);
|
||||
}else {
|
||||
} else if (!attr) {
|
||||
//tag value
|
||||
if (key === this.options.textNodeName) {
|
||||
let newval = this.options.tagValueProcessor(key, '' + jObj[key]);
|
||||
@@ -114,6 +120,7 @@ Builder.prototype.j2x = function(jObj, level) {
|
||||
//repeated nodes
|
||||
const arrLen = jObj[key].length;
|
||||
let listTagVal = "";
|
||||
let listTagAttr = "";
|
||||
for (let j = 0; j < arrLen; j++) {
|
||||
const item = jObj[key][j];
|
||||
if (typeof item === 'undefined') {
|
||||
@@ -123,17 +130,27 @@ Builder.prototype.j2x = function(jObj, level) {
|
||||
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
||||
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
||||
} else if (typeof item === 'object') {
|
||||
if(this.options.oneListGroup ){
|
||||
listTagVal += this.j2x(item, level + 1).val;
|
||||
if(this.options.oneListGroup){
|
||||
const result = this.j2x(item, level + 1, ajPath.concat(key));
|
||||
listTagVal += result.val;
|
||||
if (this.options.attributesGroupName && item.hasOwnProperty(this.options.attributesGroupName)) {
|
||||
listTagAttr += result.attrStr
|
||||
}
|
||||
}else{
|
||||
listTagVal += this.processTextOrObjNode(item, key, level)
|
||||
listTagVal += this.processTextOrObjNode(item, key, level, ajPath)
|
||||
}
|
||||
} else {
|
||||
listTagVal += this.buildTextValNode(item, key, '', level);
|
||||
if (this.options.oneListGroup) {
|
||||
let textValue = this.options.tagValueProcessor(key, item);
|
||||
textValue = this.replaceEntitiesValue(textValue);
|
||||
listTagVal += textValue;
|
||||
} else {
|
||||
listTagVal += this.buildTextValNode(item, key, '', level);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(this.options.oneListGroup){
|
||||
listTagVal = this.buildObjectNode(listTagVal, key, '', level);
|
||||
listTagVal = this.buildObjectNode(listTagVal, key, listTagAttr, level);
|
||||
}
|
||||
val += listTagVal;
|
||||
} else {
|
||||
@@ -145,7 +162,7 @@ Builder.prototype.j2x = function(jObj, level) {
|
||||
attrStr += this.buildAttrPairStr(Ks[j], '' + jObj[key][Ks[j]]);
|
||||
}
|
||||
} else {
|
||||
val += this.processTextOrObjNode(jObj[key], key, level)
|
||||
val += this.processTextOrObjNode(jObj[key], key, level, ajPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,8 +177,8 @@ Builder.prototype.buildAttrPairStr = function(attrName, val){
|
||||
} else return ' ' + attrName + '="' + val + '"';
|
||||
}
|
||||
|
||||
function processTextOrObjNode (object, key, level) {
|
||||
const result = this.j2x(object, level + 1);
|
||||
function processTextOrObjNode (object, key, level, ajPath) {
|
||||
const result = this.j2x(object, level + 1, ajPath.concat(key));
|
||||
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
|
||||
return this.buildTextValNode(object[this.options.textNodeName], key, result.attrStr, level);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user