+ Tab frame mode switching display Json / Xml format, filed Json / Xml string to the backend

1. Tab mode switching box plus

   I add a Tab, Tab become transparent can not display properly, I noxious in Modal inside. . .

   Then finally it can be displayed, and beat drums and set off firecrackers! !

 

 

 

 

 

 Code on po ~

 <!-- 模态框(Modal) Begin-->
    <div class="modal fade" id="showContent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="height:800px;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h5 class="modal-title" id="modal_ESBtitle"></h5>
                </div>
                <div class="modal-body">
                    <ul id="myTab" class="nav nav-tabs">
                        <li class="active"> <a href="#XML" data-toggle="tab">XML</a></li>
                        <li><a href="#JSON" data-toggle="tab">JSON</a></li>
                    </ul>
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane fade in active" id="XML" name="show">
                            <textarea class="form-control" rows="15" id="showXML" style="border-top-width:0px; border-radius:20px; resize:none;"></textarea>
                        </div>
                        <div class="tab-pane fade" id="JSON" name="show">
                            <textarea class="form-control" rows="15" id="showJSON" style="border-top-width:0px; border-radius:20px; resize:none;"></textarea>
                        </div>
                    </div>
                </div>
                <div class="form-group modal-footer">
                    <div class="col-sm-12" id="actionBar">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 模态框(Modal) End-->

2. The next step is to display the Xml and Json data it! ~

   Explain my side of the inventory data is xml string, so after I get the string to operate the meal fierce as a tiger. . .

// modal window 
function showTabContent (Content) {
     // Assignment 
    $ ( "# showXML" ) .text (xmlFormat (Content)); 
    $ ( . "#ShowJSON") text (the JSON.stringify (xmlStrToJsonObj (Content), null , 4 ));
     // pop 
    $ ( "# showContent") Modal ( 'Show'. ); 

} 
// formatted XML   
function xmlFormat (XML) {
     var formatted = '' ;
     var REG = / (>) ( <) (\ / *) / G; 
    XML = xml.replace (REG, '$. 1 \ R & lt \ n-$ 2 $. 3' );
     var PAD = 0 ; 
    $ .each (xml.split ('\r\n'), function (index, node) {
        var indent = 0;
        if (node.match(/.+<\/\w[^>]*>$/)) {
            indent = 0;
        } else if (node.match(/^<\/\w/)) {
            if (pad != 0) {
                pad -= 1;
            }
        } else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
            indent = 1;
        } else {
            indent = 0;
        }
        var padding = '' ;
         for ( var I = 0; I <PAD; I ++ ) { 
            padding + = '' ; 
        } 
        formatted + = padding + Node + '\ R & lt \ n-' ; 
        PAD + = indent; 
    }); 
    return formatted; 


} 
// the string to json xml objects 
function xmlStrToJsonObj (xmlStr) {
     var xmlObj = xmlStrToXmlObj (xmlStr);
     var jsonObj = {};
     IF (xmlObj.childNodes.length> 0 ) { 
        jsonObj=xmlObjToJsonObj (xmlObj.childNodes); 
    } 
    return jsonObj; 
} 
// the string to xml xml objects 
function xmlStrToXmlObj (xmlStr) { 

    var xmlObj = {};
     IF (the document.all) {
         var XMLDOM = new new the ActiveXObject ( "the Microsoft. the XMLDOM " ); 
        xmlDom.loadXML (xmlStr); 
        xmlObj = XMLDOM; 
    } the else { 
        xmlObj = new new the DOMParser () The parseFromString (xmlStr,." text / XML " ); 
    } 
    return xmlObj; 

} 
//将xml对象转为json对象
function xmlObjToJsonObj(xmlNodes) {

    var obj = {};
    if (xmlNodes.length == 0) {
        obj = '';
    } else {
        for (var i = 0; i < xmlNodes.length; i++) {
            var node = xmlNodes[i];
            if (typeof node.tagName == "undefined" || node.nodeName == "#text") {
                obj = node.nodeValue;
            } else {
                var key = node.tagName;
                var value = xmlObjToJsonObj(node.childNodes)
                obj[key] = value;
            }
        }
    }
    return obj;
}

copy click on ok friends ~ ~

3. Submit Json / Xml string

 I am here need to save xml string to the database, so I first submit data to the background, and then converted ~

 (Background judge Json / Xml format is correct, because the front-end to determine if each function browsers are not the same, it is trouble!)

   Ajax front-end submission (be sure to remove excess symbol, because when the show, plus a lot of symbols)

        // remove excess symbols (\ n \ r space) 
        Content = str.replace (/ \ n-/ G, '') .replace (/ \ R & lt / G, '') .replace (/ \ * S / G, " ");

   Code-behind (Mvc)

        // modify the contents of 
        [HttpPost]
         [ValidateInput ( false)] (impotant! Be sure to add to the data is received, or else will complain, saying that there are dangerous characters ~)
         public JsonResult EditTabXml ( String str, String strType, String Id) 
        { 
            var Data = new new DataSourceResult the Data = { "" , the Total = 0 };
             return Json (Data, JsonRequestBehavior.AllowGet); 
        } 
        ///  <Summary> 
        /// determines whether a valid xml
         ///  </ Summary> 
        ///  <param name = "STR"> XML string </ param>
        ///  <Returns> Returns null character represented correctly formatted xml </ Returns> 
        public  String isXml ( String STR) 
        { 
            String Result = "" ;
             the try 
            { 
                The System.Xml.XmlDocument DOC = new new The System.Xml.XmlDocument (); / / new objects 
                doc.LoadXml (STR); // a string in xml format 
            }
             the catch (Exception E) 
            { 
                Result = " Error: " + e.Message; 
            }
            return Result; 
        } 
        ///  <Summary> 
        /// determines whether a valid json
         ///  </ Summary> 
        ///  <param name = "STR"> json string </ param> 
        ///  <Returns> return null json character representation correctly formatted </ Returns> 
        public  String IsJson ( String STR) 
        { 
            String Result = "" ;
             the try 
            { 
                Object routes_list = new new the JavaScriptSerializer () DeserializeObject (STR).;// Switch json objects 
            }
             the catch (Exception E)
            {
                Result = " Error: " + e.Message; 
            } 
            return Result; 
        } 
        ///  <Summary> 
        /// Json string rotation Xml string
         ///  </ Summary> 
        ///  <param name = "STR"> json string </ param> 
        ///  <Returns> XML string </ Returns> 
        public  string JsonToXml ( string STR) 
        { 
            string Result = "" ;
            try
            {
                XmlDocument xmlDoc = new the XmlDocument (); 

                XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader (Encoding.UTF8.GetBytes (STR), XmlDictionaryReaderQuotas.Max); 
                xmlDoc.Load (xmlReader); 
                // JSON turn XML 
                the XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration ( " 1.0 " , " UTF-8 " , String .Empty);
                 // create xml declaration 
                xmlDoc.InsertBefore (xmlDec, xmlDoc.documentElement); // insert xml declaration
                 // must be removed, I think of any other way, only with this stupid Measures 
                result = xmlDoc.OuterXml;
                result = result.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
                result = result.Replace("type=\"string\"", "");
                result = result.Replace("type=\"object\"", "");
                result = result.Replace("type=\"boolean\"", "");
                result = result.Replace("type=\"number\"", "");
                result = result.Replace("type=\"int\"", "");
                result = result.Trim();

            }
            catch (Exception e)
            {
                result = e.Message;
            }
            return result;
        }

 

Guess you like

Origin www.cnblogs.com/MuZiLily/p/11504910.html