易简.道(ething)

爱在进行时
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JavaScript XML相关

Posted on 2010-02-28 09:33  如是如是  阅读(215)  评论(0编辑  收藏  举报
  1. function convert_string_to_xml(strXML)   
  2. {   
  3.     if (window.ActiveXObject) {   
  4.         xmlDoc=new ActiveXObject("Microsoft.XMLDOM");   
  5.         xmlDoc.async="false";   
  6.         xmlDoc.loadXML(strXML);   
  7.         return xmlDoc;   
  8.     } else {   
  9.         parser=new DOMParser();   
  10.         xmlDoc=parser.parseFromString(strXML,"text/xml");   
  11.         return xmlDoc;   
  12.     }   

 

  1. function convert_xml_to_string(xmlObject)   
  2. {   
  3.     if (window.ActiveXObject) { // for IE   
  4.         return xmlObject.xml;   
  5.     } else {   
  6.         return (new XMLSerializer()).serializeToString(xmlObject);   
  7.     }   
  8. }  

 

  1. IE
  2. <script type="text/javascript">
  3.     function load_xml_content_string(xmlData) {
  4.         if (window.ActiveXObject) {
  5.             //for IE
  6.             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  7.             xmlDoc.async="false";
  8.             xmlDoc.loadXML(xmlData);
  9.             return xmlDoc;
  10.         } else if (document.implementation && document.implementation.createDocument) {
  11.             //for Mozila
  12.             parser=new DOMParser();
  13.             xmlDoc=parser.parseFromString(xmlData,"text/xml");
  14.             return xmlDoc;
  15.         }
  16.     }
  17.     var xmlObject = load_xml_content_string("<employee><age>12</age></employee>");
  18.    
  19.     var xmlString = xmlObject.xml;
  20.     alert(string);
  21. </script>

     

  22. FF
  23. <script type="text/javascript">
  24.     function load_xml_content_string(xmlData) {
  25.         if (window.ActiveXObject) {
  26.             //for IE
  27.             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  28.             xmlDoc.async="false";
  29.             xmlDoc.loadXML(xmlData);
  30.             return xmlDoc;
  31.         } else if (document.implementation && document.implementation.createDocument) {
  32.             //for Mozila
  33.             parser=new DOMParser();
  34.             xmlDoc=parser.parseFromString(xmlData,"text/xml");
  35.             return xmlDoc;
  36.         }
  37.     }
  38.     var xmlObject = load_xml_content_string("<employee><age>12</age></employee>");
  39.    
  40.     var xmlString = (new XMLSerializer()).serializeToString(xmlObject);
  41.     alert(xmlString);
  42. </script>