/** <Br/> * Change JSON object to XML string. <br/> * The JSON object value must be a string, an array or object. <br/> * If the value is an integer or a float value, you must add "or 'to the value. <br/> * @ author bitjjj <br/> * @ Param ispretty if format XML <br/> * @ Param separator set line separator <br/> * @ example var util = new jsontoxml (true ); vaR result = util. toxml (jsonobject); <br/> * @ Re Turn <br/> */<br/> function jsontoxml (ispretty, separator) {<br/> This. Result = []; <br/> This. ispretty = !! Ispretty; <br/> This. Separator = separator | "/R/N"; </P> <p> This. Result. Push ("<? XML version =/"1.0/" encoding =/"UTF-8/"?> "); <Br/> If (this. ispretty) {<br/> // This. result. push (this. separator); <br/>}</P> <p> jsontoxml. prototype. spacialchars = ["&", "<", ">", "/" "," '"]; <br/> jsontoxml. prototype. validchars: ["& amp;", "& lt;", "& gt;", "& quot;", "& apos;"], </P> <p> jsontoxml. prototype. tostring = function () {<br/> return this. result. join (""); <br/>}; </P> <p> jsontoxml. prototype. replacespecialchar = function (s) {</P> <p> for (VAR I = 0; I <this. Spacialchars. length; I ++) {<br/> S = S. replace (New Regexp (this. spacialchars [I], "G"), this. validchars [I]); <br/>}< br/> return s; <br/>}; </P> <p> jsontoxml. prototype. appendtext = function (s) {<br/> S = This. replacespecialchar (s); <br/> This. result. push (s); <br/>}; </P> <p> jsontoxml. prototype. appendflagbegin = function (s) {</P> <p> This. result. push ("<" + S + ">"); <br/>}; </P> <p> jsontoxml. prototype. appendflagend = Function (s) {<br/> This. result. push ("</" + S + ">"); <br/> If (this. ispretty) {<br/> // This. result. push (this. separator); <br/>}< br/>}; </P> <p> jsontoxml. prototype. each = function (ARR, CB) {<br/> for (VAR I = 0; I <arr. length; I ++) {<br/> CB (I, arr [I]); <br/>}< br/> }; </P> <p>/** <br/> * Format XML string to pretty string <br/> * @ Param XML string <br/> * @ return pretty XML string <br/> * @ reference http: // stackoverf Logocom/questions/376373/pretty-printing-xml-with-javascript <br/> */<br/> jsontoxml. prototype. formatxml = function (XML) {<br/> var formatted = []; <br/> var Reg =/(>) (<) (// *)/g; <br/> xml = xml. replace (Reg, '$ 1' + this. separator + '$2 $ 3'); <br/> var pad = 0, self = This; <br/> This. each (XML. split (this. separator), function (index, node) {<br/> var indent = 0; <br/> If (node. match (/. + <// W [^>] *> $/) {<br /> Indent = 0; <br/>}< br/> else if (node. match (/^ <// w/) {<br/> If (pad! = 0) {<br/> pad-= 1; <br/>}< br/> else if (node. match (/^ </W [^>] * [^/]>. * $/) {<br/> indent = 1; <br/>}< br/> else {<br/> indent = 0; <br/>}< br/> var padding = ''; <br/> for (VAR I = 0; I <pad; I ++) {<br/> padding + = ''; <br/>}< br/> formatted. push (padding + node + self. separator); <br/> pad + = indent; <br/>}); <br/> return formatted. join (""); <br/>}; </P> <p> jsontoxml. prototype. toxml = function (JSON) {<br/> This. _ toxml (JSON); </P> <p> If (this. ispretty) {<br/> return this. formatxml (this. tostring (); <br/>}< br/> return this. tostring (); <br/>}; </P> <p> jsontoxml. prototype. _ toxml = function (JSON) {</P> <p> for (VAR tag in JSON) {<br/> // need to handle array object specially <br/> If (JSON [tag]. constructor = array) {<br/> for (VAR I = 0; I <JSON [tag]. length; I ++) {<br/> This. appendflagbegin (TAG); <br/> var item = JSON [tag] [I]; <br/> If (item. constructor = Object) {<br/> This. _ toxml (item); <br/>}< br/> else if (item. constructor = array) {<br/> var OBJ = {}; <br/> OBJ [tag] = item; <br/> This. _ toxml (OBJ); <br/>}< br/> else if (item. constructor = string) {<br/> This. appendtext (item); <br/>}< br/> This. appendflagend (TAG); <br/>}< br/> else {<br/> This. appendflagbegin (TAG); <br/> If (JSON [tag]. constructor = Object) {<br/> This. _ toxml (JSON [tag]); <br/>}< br/> else if (JSON [tag]. constructor = string) {<br/> This. appendtext (JSON [tag]); <br/>}< br/> This. appendflagend (TAG); <br/>}< br/> };