When writing web pages that work with XML, it's often a headache for browser compatibility. So I encapsulate the usual XML operations as functions. After a period of improvement, it is now very stable and comfortable to use.
function has--
Xml_loadfile:xml synchronous/Asynchronous loading.
xml_transformnode:xsl conversion.
Xml_text: The text of the node.
selectSingleNode: Select a single node based on XPath.
SelectNodes: Select multiple nodes based on XPath.
All code (zyllibjs_xml.js)--
Copy Code code as follows:
/*
Zyllibjs_xml
XML processing
@author zyl910
Note--
1. Chrome cannot read local files because of its security restrictions.
Reference
~~~~~~~~~
http://www.jinlie.net/?p=302
Chrome browser Loads XML document
Update
~~~~~~
[2011-11-02]
Defined.
[2011-11-09]
Xml_loadfile: Adds a iserror parameter to the callback function.
[2011-11-21]
selectSingleNode
SelectNodes
*/
Load XML file and return XML document node
Return: Returns an object when successful (returns an XML Document object in synchronous mode, returns an Action object in asynchronous mode), and returns NULL when failed.
The URL of the Xmlurl:xml file.
Funcasync: callback function. function onload (xmldoc, IsError) {...}
function Xml_loadfile (XMLURL, Funcasync)
{
var xmldoc = null;
var ischrome = false;
var asyncis = (Null!=funcasync); Whether it is loaded asynchronously. When Funcasync is not empty, asynchronous loading is used, otherwise it is loaded synchronously.
Check parameters
if ("" ==xmlurl) return null;
if (ASYNCIS)
{
if ("function"!=typeof (funcasync)) return null;
}
Creating XML objects
Try
{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); Support IE
}
catch (ex)
{
}
if (Null==xmldoc)
{
Try
{
Support Firefox, Mozilla, Opera, etc
xmldoc = Document.implementation.createDocument ("", "", null); Creates an empty XML Document object.
}
catch (ex)
{
}
}
if (Null==xmldoc) return null;
Load XML document
Xmldoc.async = Asyncis;
if (ASYNCIS)
{
if (window. ActiveXObject)
{
Xmldoc.onreadystatechange = function () {
if (xmldoc.readystate = 4)
{
var isError = false;
if (null!=xmldoc.parseerror)
{
IsError = (0!=xmldoc.parseerror.errorcode); 0 success, not 0 failure.
}
Funcasync (xmldoc, isError);
}
}
}
Else
{
Xmldoc.onload = function () {
Funcasync (xmldoc, false);
}
}
}
Try
{
Xmldoc.load (XMLURL);
}
catch (ex)
{
Alert (ex.message)//If the browser is chrome, it will catch this exception: Object # (a Document) has no method "load"
Ischrome = true;
xmldoc = null;
}
if (ischrome)
{
var xhr = new XMLHttpRequest ();
if (ASYNCIS)//asynchronous
{
Xhr.onreadystatechange = function () {
if (xhr.readystate = 4)
{
Funcasync (Xhr.responsexml, Xhr.status!= 200);
}
}
Xhr.open ("Get", XMLURL, True);
In try///asynchronous mode, the callback function handles the error.
{
Xhr.send (NULL);
}
catch (ex)
{
Funcasync (null, TRUE);
return null;
}
return XHR; Note: The return is XMLHttpRequest. It is recommended that you return values only with NULL tests in asynchronous mode.
}
else//Sync
{
Xhr.open ("Get", XmlUrl, false);
Xhr.send (NULL); In synchronous mode, an exception is handled by the caller
xmldoc = Xhr.responsexml;
}
}
return xmldoc;
}
Use XSLT to convert an XML document to a string.
function Xml_transformnode (xmldoc, Xsldoc)
{
if (Null==xmldoc) return "";
if (Null==xsldoc) return "";
if (window. ActiveXObject)//IE
{
Return Xmldoc.transformnode (Xsldoc);
}
else//FireFox, Chrome
{
Defining Xsltprocesor Objects
var xsltprocessor=new xsltprocessor ();
Xsltprocessor.importstylesheet (Xsldoc);
Transformtodocument Way
var result=xsltprocessor.transformtodocument (xmldoc);
var xmls=new XMLSerializer ();
var RT = Xmls.serializetostring (Result);
return RT;
}
}
Get the text of a node
function Xml_text (XmlNode)
{
if (Null==xmlnode) return "";
var RT;
if (window. ActiveXObject)//IE
{
RT = Xmlnode.text;
}
Else
{
FireFox, Chrome, ...
RT = Xmlnode.textcontent;
}
if (NULL==RT) Rt=xmlnode.nodevalue; XML DOM
return RT;
}
Add a method. To be compatible with Firefox, Chrome.
if (!window. ActiveXObject)
{
XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function (XPath)
{
var x = this.selectnodes (XPath)
if (! x | | X.length < 1) return null;
return x[0];
}
XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (XPath)
{
var xpe = new Xpathevaluator ();
var nsresolver = xpe.creatensresolver (this.ownerdocument = = Null?this.documentelement: This.ownerDocument.documentElement);
var result = Xpe.evaluate (XPath, this, nsresolver, 0, NULL);
var found = [];
var res;
while (res = Result.iteratenext ())
Found.push (RES);
return found;
}
}
Chrome Browser loads XML document
The load method is not supported by Chrome browsers to load XML documents. Look for the following online, you need to solve the following:
Copy Code code as follows:
function Loadxmldoc (xml_name)
{
var xmldoc;
Try
{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); Support IE
}
catch (E)
{
Try
{
Support Firefox, Mozilla, Opera, etc
xmldoc = Document.implementation.createDocument ("", "", null);//Create an empty XML Document object.
}
catch (E)
{
alert (e.message);
}
}
Load XML document
Try
{
Xmldoc.async = false; Turn off asynchronous loading
Xmldoc.load (Xml_name);
}
catch (E)
{
Alert (e.message) if the browser is chrome, it will catch this exception: Object # (a Document) has no method "load", so the following implementation supports the chrome loading of XML documents (just a cursory write down)
var xhr = new XMLHttpRequest ();
Xhr.open ("Get", Xml_name, false);
Xhr.send (NULL);
xmldoc = xhr.responseXML.documentElement;
}
return xmldoc;
}
BTW, each browser loads an XML string differently.
IE uses the Loadxml () method to parse XML strings:
Copy Code code as follows:
Xmldoc.loadxml (XML_STR);
Firefox, and so on using the Domparser object to parse XML strings:
Copy Code code as follows:
var parsexml = new Domparser ();
var doc = parsexml.parsefromstring (xml_str, "text/xml");