Skip to main content

הפיכת XML ל - JSON

דינוזאורים עדיין עובדים על XML בכל מיני מקומות לצערי. זה נכון XML נותן הרבה יותר יכולות ש - JSON לא, אבל עדיין, JSON זה פשוט כיף.

לעיתים אנחנו נתקלים בצורך להמיר XML ל - JSON מתוך JavaScript, כמו לדוגמא בשימוש בתוך Firefox Extension או קריאות Ajax ו - XSS Cross Sites ּScripting.

ישנן הרבה ספריות ברשת להמרת XML ל - JSON רק חלקן ב - JavaScript ורובן לא עובדות.
מצאתי אחת שכן עובדת:
http://michael.hinnerup.net/blog/wp-content/uploads/2008/01/JsonXml.js
http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/



/*
The below work is licensed under Creative Commons GNU LGPL License.

Original work:

License: http://creativecommons.org/licenses/LGPL/2.1/
Author: Stefan Goessner/2006
Web: http://goessner.net/

Modifications made:

Version: 0.9-p5
Description: Restructured code, JSLint validated (no strict whitespaces),
added handling of empty arrays, empty strings, and int/floats values.
Author: Michael Schøler/2008-01-29
Web: http://michael.hinnerup.net/blog/2008/01/26/converting-json-to-xml-and-xml-to-json/
*/

/*global alert */
var xmlJsonClass = {
// Param "xml": Element or document DOM node.
// Param "tab": Tab or indent string for pretty output formatting omit or use empty string "" to supress.
// Returns: JSON string
xml2json: function(xml, tab) {
if (xml.nodeType === 9) {
// document node
xml = xml.documentElement;
}
var nws = this.removeWhite(xml);
var obj = this.toObj(nws);
var json = this.toJson(obj, xml.nodeName, "\t");
return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";
},

// Param "o": JavaScript object
// Param "tab": tab or indent string for pretty output formatting omit or use empty string "" to supress.
// Returns: XML string
json2xml: function(o, tab) {
var toXml = function(v, name, ind) {
var xml = "";
var i, n;
if (v instanceof Array) {
if (v.length === 0) {
xml += ind + "<"+name+">__EMPTY_ARRAY_\n";
}
else {
for (i = 0, n = v.length; i < n; i += 1) {
var sXml = ind + toXml(v[i], name, ind+"\t") + "\n";
xml += sXml;
}
}
}
else if (typeof(v) === "object") {
var hasChild = false;
xml += ind + "<" + name;
var m;
for (m in v) if (v.hasOwnProperty(m)) {
if (m.charAt(0) === "@") {
xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
}
else {
hasChild = true;
}
}
xml += hasChild ? ">" : "/>";
if (hasChild) {
for (m in v) if (v.hasOwnProperty(m)) {
if (m === "#text") {
xml += v[m];
}
else if (m === "#cdata") {
xml += "";
}
else if (m.charAt(0) !== "@") {
xml += toXml(v[m], m, ind+"\t");
}
}
xml += (xml.charAt(xml.length - 1) === "\n" ? ind : "") + "";
}
}
else {
if (v.toString() === "\"\"" || v.toString().length === 0) {
xml += ind + "<" + name + ">__EMPTY_STRING_";
}
else {
xml += ind + "<" + name + ">" + v.toString() + "";
}
}
return xml;
};
var xml = "";
var m;
for (m in o) if (o.hasOwnProperty(m)) {
xml += toXml(o[m], m, "");
}
return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
},

// Internal methods
toObj: function(xml) {
var o = {};
if (xml.nodeType === 1) {
// element node ..
if (xml.attributes.length) {
// element with attributes ..
var i;
for (i = 0; i < xml.attributes.length; i += 1) {
o["@" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || "").toString();
}
}
if (xml.firstChild) {
// element has child nodes ..
var textChild = 0, cdataChild = 0, hasElementChild = false;
var n;
for (n = xml.firstChild; n; n = n.nextSibling) {
if (n.nodeType === 1) {
hasElementChild = true;
}
else if (n.nodeType === 3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
// non-whitespace text
textChild += 1;
}
else if (n.nodeType === 4) {
// cdata section node
cdataChild += 1;
}
}
if (hasElementChild) {
if (textChild < 2 && cdataChild < 2) {
// structured element with evtl. a single text or/and cdata node ..
this.removeWhite(xml);
for (n = xml.firstChild; n; n = n.nextSibling) {
if (n.nodeType === 3) {
// text node
o["#text"] = this.escape(n.nodeValue);
}
else if (n.nodeType === 4) {
// cdata node
o["#cdata"] = this.escape(n.nodeValue);
}
else if (o[n.nodeName]) {
// multiple occurence of element ..
if (o[n.nodeName] instanceof Array) {
o[n.nodeName][o[n.nodeName].length] = this.toObj(n);
}
else {
o[n.nodeName] = [o[n.nodeName], this.toObj(n)];
}
}
else {
// first occurence of element ..
o[n.nodeName] = this.toObj(n);
}
}
}
else {
// mixed content
if (!xml.attributes.length) {
o = this.escape(this.innerXml(xml));
}
else {
o["#text"] = this.escape(this.innerXml(xml));
}
}
}
else if (textChild) {
// pure text
if (!xml.attributes.length) {
o = this.escape(this.innerXml(xml));
if (o === "__EMPTY_ARRAY_") {
o = "[]";
} else if (o === "__EMPTY_STRING_") {
o = "";
}
}
else {
o["#text"] = this.escape(this.innerXml(xml));
}
}
else if (cdataChild) {
// cdata
if (cdataChild > 1) {
o = this.escape(this.innerXml(xml));
}
else {
for (n = xml.firstChild; n; n = n.nextSibling) {
o["#cdata"] = this.escape(n.nodeValue);
}
}
}
}
if (!xml.attributes.length && !xml.firstChild) {
o = null;
}
}
else if (xml.nodeType === 9) {
// document.node
o = this.toObj(xml.documentElement);
}
else {
alert("unhandled node type: " + xml.nodeType);
}
return o;
},
toJson: function(o, name, ind) {
var json = name ? ("\"" + name + "\"") : "";
if (o === "[]") {
json += (name ? ":[]" : "[]");
}
else if (o instanceof Array) {
var n, i;
for (i = 0, n = o.length; i < n; i += 1) {
o[i] = this.toJson(o[i], "", ind + "\t");
}
json += (name ? ":[" : "[") + (o.length > 1 ? ("\n" + ind + "\t" + o.join(",\n" + ind + "\t") + "\n" + ind) : o.join("")) + "]";
}
else if (o === null) {
json += (name && ":") + "null";
}
else if (typeof(o) === "object") {
var arr = [];
var m;
for (m in o) if (o.hasOwnProperty(m)) {
arr[arr.length] = this.toJson(o[m], m, ind + "\t");
}
json += (name ? ":{" : "{") + (arr.length > 1 ? ("\n" + ind + "\t" + arr.join(",\n" + ind + "\t") + "\n" + ind) : arr.join("")) + "}";
}
else if (typeof(o) === "string") {
o = o.toString();
var objRegExp = /(^-?\d+\.?\d*$)/;
if (objRegExp.test(o)) {
// int or float
json += (name && ":") + o;
}
else {
json += (name && ":") + "\"" + o + "\"";
}
}
else {
json += (name && ":") + o.toString();
}
return json;
},
innerXml: function(node) {
var s = "";
if ("innerHTML" in node) {
s = node.innerHTML;
}
else {
var asXml = function(n) {
var s = "", i;
if (n.nodeType === 1) {
s += "<" + n.nodeName;
for (i = 0; i < n.attributes.length; i += 1) {
s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue || "").toString() + "\"";
}
if (n.firstChild) {
s += ">";
for (var c = n.firstChild; c; c = c.nextSibling) {
s += asXml(c);
}
s += "";
}
else {
s += "/>";
}
}
else if (n.nodeType === 3) {
s += n.nodeValue;
}
else if (n.nodeType === 4) {
s += "";
}
return s;
};
for (var c = node.firstChild; c; c = c.nextSibling) {
s += asXml(c);
}
}
return s;
},
escape: function(txt) {
return txt.replace(/[\\]/g, "\\\\").replace(/[\"]/g, '\\"').replace(/[\n]/g, '\\n').replace(/[\r]/g, '\\r');
},
removeWhite: function(e) {
e.normalize();
var n;
for (n = e.firstChild; n; ) {
if (n.nodeType === 3) {
// text node
if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) {
// pure whitespace text node
var nxt = n.nextSibling;
e.removeChild(n);
n = nxt;
}
else {
n = n.nextSibling;
}
}
else if (n.nodeType === 1) {
// element node
this.removeWhite(n);
n = n.nextSibling;
}
else {
// any other node
n = n.nextSibling;
}
}
return e;
}
};


Comments

Popular posts from this blog

Using phpword to merge two Mircrosoft Office Word .docx documents

How to combine or embed and insert another .docx file (Microsoft office docx word document) into another one using PHPWord Joining two .docx document using php ( phpword library ) $mainTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file1"); //$mainTemplateProcessor ->setValue('var_name', $value); $innerTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file2"); //$innerTemplateProcessor->setValue('var2_name', $value2); // extract internal xml from template that will be merged inside main template $innerXml = $innerTemplateProcessor->gettempDocumentMainPart(); $innerXml = preg_replace('/^[\s\S]*<w:body>(.*)<\/w:body>.*/', '$1', $innerXml); // remove tag containing header, footer, images $innerXml = preg_replace('/<w:sectPr>.*<\/w:sectPr>/', '', $innerXml); // inject internal xml inside main template $mainXml = $mainTemplateProcessor->gettempDocumentMainPart(...

Bypassing the error by "go get" "tls: failed to verify certificate: x509: certificate signed by unknown authority"

When I was trying to download dependencies for my go project in an old Ubuntu machine I was getting this error all the time: "go: gopkg.in/alexcesaro/quotedprintable.v3@v3.0.0-20150716171945-2caba252f4dc: Get "https://proxy.golang.org/gopkg.in/alexcesaro/quotedprintable.v3/@v/v3.0.0-20150716171945-2caba252f4dc.mod": tls: failed to verify certificate: x509: certificate signed by unknown authority" Which the main part of it was go get failing to authenticate: " tls: failed to verify certificate: x509: certificate signed by unknown authority " I tried many things but couldn't make it work until I found the way: export GOINSECURE="proxy.golang.go" This will tell go get to ignore certification validity. Then export GOPROXY=direct This will tell go get to by pass proxy Then git config --global http.sslverify false And only after those I could run again: go get And it worked

Accessing Windows Share (Samba) From Linux (XFCE) using Thunar

In order to access samba (windows share) from Linux XFCE using Thunar file browser you need to: 1. Install samba client sudo apt install smbclient 2. Run: sudo modprobe cifs 3. Open thunar and access the share: smb://IPADDRESS/sharename e.g.: smb://192.168.1.1/photos 4. Type in the username / password in the prompt window Some helpful information: https://forum.manjaro.org/t/thunar-how-do-i-access-folders-shared-on-windows-computers/116649/9