Learning JSON: format conversion from XML to JSON

Source: Internet
Author: User
Tags tojson
Function xmltojson (XML, Tab ){
VaR x = {
Toobj: function (XML ){
VaR o = {};
If (XML. nodetype = 1) {// Element Node ..
If (XML. Attributes. Length) // element with attributes ..
For (VAR I = 0; I <XML. Attributes. length; I ++)
O ["@" + XML. attributes [I]. nodename] = (XML. attributes [I]. nodevalue | ""). tostring ();
If (XML. firstchild) {// element has child nodes ..
VaR textchild = 0, cdatachild = 0, haselementchild = false;
For (VAR n = xml. firstchild; n = n. nextsibling ){
If (N. nodetype = 1) haselementchild = true;
Else if (N. nodetype = 3 & N. nodevalue. match (/[^/f/n/R/T/V]/) textchild ++; // non-whitespace text
Else if (N. nodetype = 4) cdatachild ++; // CDATA section Node
}
If (haselementchild ){
If (textchild <2 & cdatachild <2) {// structured element with evtl. A single text or/and CDATA node ..
X. removewhite (XML );
For (VAR n = xml. firstchild; n = n. nextsibling ){
If (N. nodetype = 3) // text node
O ["# text"] = x. Escape (N. nodevalue );
Else if (N. nodetype = 4) // CDATA Node
O ["# CDATA"] = x. 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] = x. toobj (N );
Else
O [n. nodename] = [O [n. nodename], X. toobj (n)];
}
Else // first occurence of element ..
O [n. nodename] = x. toobj (N );
}
}
Else {// mixed content
If (! XML. Attributes. length)
O = x. Escape (X. innerxml (XML ));
Else
O ["# text"] = x. Escape (X. innerxml (XML ));
}
}
Else if (textchild) {// pure text
If (! XML. Attributes. length)
O = x. Escape (X. innerxml (XML ));
Else
O ["# text"] = x. Escape (X. innerxml (XML ));
}
Else if (cdatachild) {// CDATA
If (cdatachild> 1)
O = x. Escape (X. innerxml (XML ));
Else
For (VAR n = xml. firstchild; n = n. nextsibling)
O ["# CDATA"] = x. Escape (N. nodevalue );
}
}
If (! XML. Attributes. Length &&! XML. firstchild) O = NULL;
}
Else if (XML. nodetype = 9) {// document. Node
O = x.toobj(xml.doc umentelement );
}
Else
Alert ("unhandled node type:" + XML. nodetype );
Return O;
},
Tojson: function (O, name, IND ){
VaR JSON = Name? ("/" "+ Name + "/""):"";
If (O instanceof array ){
For (VAR I = 0, n = O. length; I <n; I ++)
O [I] = x. 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 = [];
For (VAR m in O)
Arr [arr. Length] = x. 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 ")
JSON + = (Name & ":") + "/" "+ O. tostring () + "/"";
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 = "";
If (N. nodetype = 1 ){
S + = "<" + N. nodename;
For (VAR I = 0; I <n. Attributes. length; I ++)
S + = "" + N. attributes [I]. nodename + "=/" "+ (n. attributes [I]. nodevalue | ""). tostring () + "/"";
If (N. firstchild ){
S + = "> ";
For (VAR c = n. firstchild; C = C. nextsibling)
S + = asxml (C );
S + = "</" + N. nodename + "> ";
}
Else
S + = "/> ";
}
Else if (N. nodetype = 3)
S + = n. nodevalue;
Else if (N. nodetype = 4)
S + = "<! [CDATA ["+ N. nodevalue +"]> ";
Return S;
};
For (VAR c = node. firstchild; 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 ();
For (VAR 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
X. removewhite (N );
N = n. nextsibling;
}
Else // any other Node
N = n. nextsibling;
}
Return E;
}
};
If (XML. nodetype = 9) // document Node
Xml = xml.doc umentelement;
VaR JSON = x. tojson (X. toobj (X. removewhite (XML), XML. nodename, "/t ");
Return "{/N" + TAB + (Tab? JSON. Replace (// t/g, Tab): JSON. Replace (// T |/n/g, "") + "/n }";
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.