function isValueText(obj) {
|
const objName = obj.nodeName //obj.firstChild.nodeName
|
let objNodes = obj.childNodes //obj.firstChild.childNodes
|
|
let objKeys = Object.keys(objNodes);
|
return (objName === '#text') || ((objKeys.length !== 0) && (objNodes[objKeys[0]].nodeType === 3));
|
}
|
|
$.extend({
|
// converts xml documents and xml text to json object
|
json2xml: function (jsonObj, config) {
|
config = config || {};
|
initConfigDefaults();
|
|
return parseJSONObject(jsonObj);
|
|
function initConfigDefaults() {
|
if (config.escapeMode === undefined) {
|
config.escapeMode = true;
|
}
|
config.attributePrefix = config.attributePrefix || "_";
|
config.arrayAccessForm = config.arrayAccessForm || "none";
|
config.emptyNodeForm = config.emptyNodeForm || "text";
|
if (config.enableToStringFunc === undefined) {
|
config.enableToStringFunc = true;
|
}
|
config.arrayAccessFormPaths = config.arrayAccessFormPaths || [];
|
if (config.skipEmptyTextNodesForObj === undefined) {
|
config.skipEmptyTextNodesForObj = true;
|
}
|
if (config.stripWhitespaces === undefined) {
|
config.stripWhitespaces = true;
|
}
|
config.datetimeAccessFormPaths = config.datetimeAccessFormPaths || [];
|
}
|
|
function startTag(jsonObj, element, attrList, closed) {
|
var resultStr = "<" + ((jsonObj != null && jsonObj.__prefix != null) ? (jsonObj.__prefix + ":") : "") + element;
|
if (attrList != null) {
|
for (var aidx = 0; aidx < attrList.length; aidx++) {
|
var attrName = attrList[aidx];
|
var attrVal = jsonObj[attrName];
|
if (config.escapeMode)
|
attrVal = escapeXmlChars(attrVal);
|
resultStr += " " + attrName.substr(config.attributePrefix.length) + "='" + attrVal + "'";
|
}
|
}
|
if (!closed)
|
resultStr += ">";
|
else
|
resultStr += "/>";
|
return resultStr;
|
}
|
|
function endTag(jsonObj, elementName) {
|
return "</" + (jsonObj.__prefix != null ? (jsonObj.__prefix + ":") : "") + elementName + ">";
|
}
|
|
function endsWith(str, suffix) {
|
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
}
|
|
function escapeXmlChars(str) {
|
if (typeof (str) == "string")
|
return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g, '/');
|
else
|
return str;
|
}
|
|
function jsonXmlSpecialElem(jsonObj, jsonObjField) {
|
if ((config.arrayAccessForm == "property" && endsWith(jsonObjField.toString(), ("_asArray")))
|
|| jsonObjField.toString().indexOf(config.attributePrefix) == 0
|
|| jsonObjField.toString().indexOf("__") == 0
|
|| (jsonObj[jsonObjField] instanceof Function))
|
return true;
|
else
|
return false;
|
}
|
|
function jsonXmlElemCount(jsonObj) {
|
var elementsCnt = 0;
|
if (jsonObj instanceof Object) {
|
for (var it in jsonObj) {
|
if (jsonXmlSpecialElem(jsonObj, it))
|
continue;
|
elementsCnt++;
|
}
|
}
|
return elementsCnt;
|
}
|
|
function parseJSONObject(jsonObj) {
|
var result = "";
|
|
var elementsCnt = jsonXmlElemCount(jsonObj);
|
|
if (elementsCnt > 0) {
|
for (var it in jsonObj) {
|
|
if (jsonXmlSpecialElem(jsonObj, it))
|
continue;
|
|
var subObj = jsonObj[it];
|
|
var attrList = parseJSONAttributes(subObj)
|
|
if (subObj == null) {
|
result += startTag(subObj, it, attrList, true);
|
} else if (subObj instanceof Object) {
|
|
if (subObj instanceof Array) {
|
result += parseJSONArray(subObj, it, attrList);
|
} else if (subObj instanceof Date) {
|
result += startTag(subObj, it, attrList, false);
|
result += subObj.toISOString();
|
result += endTag(subObj, it);
|
} else {
|
var subObjElementsCnt = jsonXmlElemCount(subObj);
|
if (subObjElementsCnt > 0 || subObj.__text != null || subObj.__cdata != null) {
|
result += startTag(subObj, it, attrList, false);
|
result += parseJSONObject(subObj);
|
result += endTag(subObj, it);
|
} else {
|
result += startTag(subObj, it, attrList, true);
|
}
|
}
|
} else {
|
result += startTag(subObj, it, attrList, false);
|
result += parseJSONTextObject(subObj);
|
result += endTag(subObj, it);
|
}
|
}
|
}
|
result += parseJSONTextObject(jsonObj);
|
|
return result;
|
}
|
|
function parseJSONAttributes(jsonObj) {
|
var attrList = [];
|
if (jsonObj instanceof Object) {
|
for (var ait in jsonObj) {
|
if (ait.toString().indexOf("__") == -1 && ait.toString().indexOf(config.attributePrefix) == 0) {
|
attrList.push(ait);
|
}
|
}
|
}
|
return attrList;
|
}
|
|
function parseJSONTextAttrs(jsonTxtObj) {
|
var result = "";
|
|
if (jsonTxtObj.__cdata != null) {
|
result += "<![CDATA[" + jsonTxtObj.__cdata + "]]>";
|
}
|
|
if (jsonTxtObj.__text != null) {
|
if (config.escapeMode)
|
result += escapeXmlChars(jsonTxtObj.__text);
|
else
|
result += jsonTxtObj.__text;
|
}
|
return result;
|
}
|
|
function parseJSONTextObject(jsonTxtObj) {
|
var result = "";
|
|
if (jsonTxtObj instanceof Object) {
|
result += parseJSONTextAttrs(jsonTxtObj);
|
} else if (jsonTxtObj != null) {
|
if (config.escapeMode)
|
result += escapeXmlChars(jsonTxtObj);
|
else
|
result += jsonTxtObj;
|
}
|
|
return result;
|
}
|
|
function parseJSONArray(jsonArrRoot, jsonArrObj, attrList) {
|
var result = "";
|
if (jsonArrRoot.length == 0) {
|
result += startTag(jsonArrRoot, jsonArrObj, attrList, true);
|
} else {
|
for (var arIdx = 0; arIdx < jsonArrRoot.length; arIdx++) {
|
result += startTag(jsonArrRoot[arIdx], jsonArrObj, parseJSONAttributes(jsonArrRoot[arIdx]), false);
|
result += parseJSONObject(jsonArrRoot[arIdx]);
|
result += endTag(jsonArrRoot[arIdx], jsonArrObj);
|
}
|
}
|
return result;
|
}
|
|
},
|
xml2json: function parseXmlToJson(xml) {
|
const json = {};
|
for (const res of xml.matchAll(/(?:<(\w*)(?:\s[^>]*)*>)((?:(?!<\1).)*)(?:<\/\1>)|<(\w*)(?:\s*)*\/>/gm)) {
|
const key = res[1] || res[3];
|
const value = res[2] && parseXmlToJson(res[2]);
|
json[key] = ((value && Object.keys(value).length) ? value : res[2]) || null;
|
|
}
|
return json;
|
},
|
isXmlValid: function (xml) {
|
try {
|
const jsonObj = this.xml2json(xml)
|
const jsonStr = JSON.stringify(jsonObj)
|
const newJsonObj = JSON.parse(jsonStr)
|
if (jsonObj && jsonStr && newJsonObj) {
|
return true;
|
}
|
} catch (err) {
|
console.log("errore nella validazione del xml: " + err)
|
return err
|
}
|
return false
|
}
|
});
|