| Xml.newdocument = function (Roottagname, namespaceurl) { if (!roottagname) roottagname = ""; if (!namespaceurl) Namespaceurl = ""; if (document.implementation && document.implementation.createDocument) { This is the "standard way to do it" Return Document.implementation.createDocument (Namespaceurl, Roottagname, NULL); } else {//This are the IE way to do it Create a empty document as an ActiveX object If there is no root element, this is all we have to do var doc = new ActiveXObject ("MSXML2. DOMDocument "); //If There is a root tag, initialize the document &NBSP;&NBSP;&NBSP;&N bsp; if (roottagname) { // Look for a namespace prefix var prefix = ""; var tagname = roottagname; var p = roottagname.indexof (': '); if (p!=-1) { prefix = roottagname.substring (0, p); tagname = Roottagname.substring (p+1); } If we have a namespace, we must have a namespace prefix If we don ' t have a namespace, we discard any prefix if (Namespaceurl) { if (!prefix) prefix = "A0"; What Firefox uses } else prefix = ""; Create the root element (with optional namespace) as a string of text var text = "<" + (prefix? prefix+ ":"): "") + tagname + (Namespaceurl ? ("xmlns:" + prefix + ' = "' + Namespaceurl + '") :"") + "/>"; and parse that text into the empty document Doc.loadxml (text); } return doc; } }; function Loadfromurl (URL) { Create a new document with the previously defined function var xmldoc = xml.newdocument (); Xmldoc.async = false; We want to load synchronously Xmldoc.load (URL); Load and Parse return xmldoc; Return the document } function xmltostring (xmldoc) { var xmlstring; Try { if (navigator.appname = = "Microsoft Internet Explorer") { xmlstring = Xmldoc.xml; } Else { xmlstring = new XMLSerializer (). serializetostring (xmldoc); } } catch (E) { xmlstring = null; } return xmlstring; } function Stringtoxmldoc (str) { var xmldoc = null; Try { var xmldomobj = new ActiveXObject ("Microsoft.XMLDOM"); Xmldomobj.async = false; Xmldomobj.loadxml (str); xmldoc = Xmldomobj; } catch (E) { Try { var domparser = new Domparser; xmldoc = domparser.parsefromstring (str, ' text/xml '); } catch (E) { xmldoc = null; } } return xmldoc; } function Stringtoxmldoc (str) { if (typeof domparser!= "undefined") { Mozilla, Firefox, and related browsers Return (new Domparser ()). parsefromstring (Text, "Application/xml"); } else if (typeof activexobject!= "undefined") { Internet Explorer. var doc = xml.newdocument (); Create an empty document Doc.loadxml (text); Parse text into it return doc; return it } else { As a resort, try loading the document from a Data:url This is supposed to work in Safari. To Manos Batsis and His Sarissa library (sarissa.sourceforge.net) is for this technique. var url = "Data:text/xml;charset=utf-8," + encodeuricomponent (text); var request = new XMLHttpRequest (); Request.open ("Get", url, false); Request.send (NULL); return request.responsexml; } } </script> |