[JavaScript] XML processing functions compatible with IE, Firefox, chrome, and other browsers (XML synchronous/asynchronous loading, XSL conversion, selectsinglenode, selectnodes)

Source: Internet
Author: User
Tags xsl

When writing web pages that process XML, it is often a headache for browser compatibility. So I encapsulate common XML operations as functions. After a period of improvement, it is very stable and comfortable to use.

 

Functions have --

Xml_loadfile: XML synchronous/asynchronous loading.

Xml_transformnode: XSL conversion.

Xml_text: the node text.

Selectsinglenode: select a single node based on XPath.

Selectnodes: Select multiple nodes based on XPath.

 

All code (zyllibjs_xml.js )--

/*

Zyllibjs_xml
XML Processing
@ Author zyl910

Note --
1. Chrome cannot read local files due to its security mechanism restrictions.


Reference
~~~~~~~~~

Http://www.jinlie.net /? P = 302
XML file loading in chrome


Update
~~~~~~

[2011-11-02]
Definition.

[2011-11-09]
Xml_loadfile: add the iserror parameter to the callback function.

[2011-11-21]
Selectsinglenode
Selectnodes

*/

// Load the XML file and return the XML file Node
// Return: an object is returned when the operation succeeds (XML Document Object is returned in synchronous mode and operation object is returned in asynchronous mode). If the operation fails, null is returned.
// Xmlurl: the URL of the 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 asynchronously loaded. When funcasync is not empty, asynchronous loading is used; otherwise, synchronous loading is used.

// Check parameters
If ("" = xmlurl) return NULL;
If (asyncis)
{
If ("function "! = Typeof (funcasync) return NULL;
}

// Create an XML Object
Try
{
Xmldoc = new activexobject ("Microsoft. xmldom"); // support IE
}
Catch (Ex)
{
}
If (null = xmldoc)
{
Try
{
// Support Firefox, Mozilla, opera, etc
Xmldoc = Document. Implementation. createdocument ("", "", null); // create an empty XML Document Object.
}
Catch (Ex)
{
}
}
If (null = xmldoc) return NULL;

// Load the 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 succeeded, not 0 failed.
}
Funcasync (xmldoc, iserror );
}
}
}
Else
{
Xmldoc. onload = function (){
Funcasync (xmldoc, false );
}
}
}
Try
{
Xmldoc. Load (xmlurl );
}
Catch (Ex)
{
// Alert (ex. Message) // If the browser is chrome, the exception will be caught: 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, errors are handled by the callback function.
{
Xhr. Send (null );
}
Catch (Ex)
{
Funcasync (null, true );
Return NULL;
}
Return xhr; // Note: XMLHttpRequest is returned. We recommend that you use null to test the return value in asynchronous mode.
}
Else // Synchronization
{
Xhr. Open ("get", xmlurl, false );
Xhr. Send (null); // The caller can handle exceptions in synchronization mode.
Xmldoc = xhr. responsexml;
}
}

Return xmldoc;
}

// Use XSLT to convert an XML document into a string.
Function xml_transformnode (xmldoc, invalid DOC)
{
If (null = xmldoc) Return "";
If (null = callback DOC) Return "";

If (window. activexobject) // IE
{
Return xmldoc. transformnode (invalid DOC );
}
Else // Firefox, chrome
{
// Define the effectprocesor object
VaR effectprocessor = new effectprocessor ();
Effectprocessor. importstylesheet (invalid DOC );
// Transformtodocument Method
VaR result = effectprocessor. transformtodocument (xmldoc );
VaR xmls = new xmlserializer ();
VaR RT = xmls. serializetostring (result );
Return RT;
}
}

// Obtain the node text
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 method. To be compatible with Firefox and 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.doc umentelement: this.ownerdocument.doc umentelement );
VaR result = xpe. Evaluate (XPath, this, nsresolver, 0, null );
VaR found = [];
VaR res;
While (RES = result. iteratenext ())
Found. Push (RES );
Return found;
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.