Supports XML reading in Firefox

Source: Internet
Author: User
Tags xml example xpath
Recently, I was modifying the page on which the Ajax function was used in the project. I found that many of the statements were problematic in Firefox, mainly because I submitted them only after I passed the test in IE, the Firefox method differs greatly from IE. The main problem is that when reading the content of an XML node or a subnode, The selectnodes and selectsinglenode methods are generally used in IE, firefox does not have these methods, so it cannot be used. After finding them for a long time a few days ago, I have not found a good solution. Many websites do not provide support at all, I finally found a good solution today. I made it into a JS file and can use it like ie If I reference it.

The Code is as follows: VaR getnodevalue = function (OBJ)
{
VaR STR = "";
If (window. activexobject) // IE
{
STR = obj. text;
}
Else // Mozilla
{
Try
{
STR = obj. childnodes [0]. nodevalue;
}
Catch (Ex)
{
STR = "";
}
}
Return STR;
}

If (document. Implementation & document. Implementation. createdocument)
{
Xmldocument. Prototype. loadxml = function (xmlstring)
{
VaR childnodes = This. childnodes;
For (VAR I = childnodes. Length-1; I> = 0; I --)
This. removechild (childnodes [I]);

VaR dp = new domparser ();
VaR newdom = DP. parsefromstring (xmlstring, "text/XML ");
VaR newelt = this.importnode(newdom.doc umentelement, true );
This. appendchild (newelt );
};

// Check for xpath implementation
If (document. Implementation. hasfeature ("XPath", "3.0 "))
{
// Prototying the xmldocument
Xmldocument. Prototype. selectnodes = function (cxpathstring, xnode)
{
If (! Xnode) {xnode = This ;}
VaR onsresolver = this.creatensresolver(this.doc umentelement)
VaR aitems = This. Evaluate (cxpathstring, xnode, onsresolver,
Xpathresult. ordered_node_snapshot_type, null)
VaR aresult = [];
For (VAR I = 0; I <aitems. snapshotlength; I ++)
{
Aresult [I] = aitems. snapshotitem (I );
}
Return aresult;
}

// Prototying the element
Element. Prototype. selectnodes = function (cxpathstring)
{
If (this. ownerdocument. selectnodes)
{
Return this. ownerdocument. selectnodes (cxpathstring, this );
}
Else {Throw "for XML elements only ";}
}
}

// Check for xpath implementation
If (document. Implementation. hasfeature ("XPath", "3.0 "))
{
// Prototying the xmldocument
Xmldocument. Prototype. selectsinglenode = function (cxpathstring, xnode)
{
If (! Xnode) {xnode = This ;}
VaR xitems = This. selectnodes (cxpathstring, xnode );
If (xitems. length> 0)
{
Return xitems [0];
}
Else
{
Return NULL;
}
}

// Prototying the element
Element. Prototype. selectsinglenode = function (cxpathstring)
{
If (this. ownerdocument. selectsinglenode)
{
Return this. ownerdocument. selectsinglenode (cxpathstring, this );
}
Else {Throw "for XML elements only ";}
}
}
}

As long as the above Code is saved as a JS file and referenced on the page, the read operation of the XML node can be used like IE and has passed the test.

Firefox XML Reading Class: http://www.cnblogs.com/Files/huacn/jquery.xml.js
Firefox read XML demo address: http://www.wathon.com/opensource/js/xmlrssreader/xmlrssreader.html
Firefox read XML example source code package download: http://www.wathon.com/opensource/js/xmlrssreader/xmlrssreader.zip

 

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.