Javascript (AJAX) XML parsing code (compatible with FIREFOX/IE) _ javascript skills

Source: Internet
Author: User
JavascriptAJAX parses XML Code (compatible with FIREFOXIE) The Code is as follows:


// Import the js File
Function getResult (url, ready ){
Var xmlHttp;
Var r = function (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
// Alert (isIE );
Var xmlstr;
Var xmldoc;
Var isIE = !! (Window. attachEvent &&! Window. opera );
If (isIE)
Xmldoc = xmlHttp. responseXML;
Else {
Xmlstr = xmlHttp. responseText;
Alert (xmlstr );
Var parser = new DOMParser ();
Xmldoc = parser. parseFromString (xmlstr, "text/xml ");
}
Try {
Ready (xmldoc );
} Catch (e ){
Alert (e. message );
}
}
}
}
Var create = function (url, r ){
Try {
// Firefox, Opera 8.0 +, Safari
XmlHttp = new XMLHttpRequest ();
} Catch (e ){
// Internet Explorer
Try {
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){
Alert ("your browser does not support AJAX! ");
Return false;
}
}
}
XmlHttp. onreadystatechange = r;
XmlHttp. open ("GET", url, true );
XmlHttp. send (null );
}
Create (url, r );
}

// Application
Function ready (xmlDoc)
{

X = xmlDoc. getElementsByTagName ("a") [1];
Y = x. childNodes [0];
Txt = y. nodeValue;
Alert (txt );
}
GetResult ("../xml. jsp", ready );


Xml:

The Code is as follows:



Ff
Ggd


The childNodes attribute returns the list of child nodes. Each element has only one subnode, that is, a text node.
The following code retrieves the text node of an element:
X = xmlDoc. getElementsByTagName ("a") [0];
Y = x. childNodes [0];
The nodeValue attribute returns the text value of the text node:
X = xmlDoc. getElementsByTagName ("title") [0];
Y = x. childNodes [0];
Txt = y. nodeValue;
Result: txt = "ggd"

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.