Introduction to XML example parsing through ajax js and JQuery _ javascript skills

Source: Internet
Author: User
Tags xml example
There are many ways to parse XML. In this article, we will introduce the following methods: ajax js and JQuery. The specific implementation is as follows, if you are interested, refer to the JQuery version.

The Code is as follows:


$. Ajax ({
Url: "order/order_orderDetail.do? Params. type = merge ",
Type: "post ",
Data: params,
Success: function (xml ){
Hide ();
If (xml = ""){
Dialog. popTip ("the order to be merged cannot be found", 2 );
} Else {
Var myTable = document. getElementById ("t_product ");
// Traverse the "ORDER" Node
$ (Xml). find ('order'). each (function (){
Var id = $ (this). find ("ORDERID"). text ();
Var status = $ (this). find ("STATUS"). text ();

If (status = "1 "){
Status = "unconfirmed ";
} Else {
Status = "Confirmed ";
}

Var newRow = myTable. insertRow ();
Var oCell = newRow. insertCell ();
OCell. setAttribute ("height", "25 ");
OCell. innerHTML = "";
OCell = newRow. insertCell ();
OCell. innerHTML = "*The status of the order "+ id +" is: "+ status +"The product information is as follows:";
OCell = newRow. insertCell ();
OCell. innerHTML = "";
OCell = newRow. insertCell ();
OCell. innerHTML = "";
// Traverse the "PRODUCT" Node
$ (This). find ('product'). each (function (){
Var pid = $ (this). find ("PRODUCTID"). text ();
Var pname = $ (this). find ("PRODUCTNAME"). text ();
Var purl = $ (this). find ("PRODUCTURL"). text ();
Var pprice = $ (this). find ("PRICE"). text ();
Var pcount = $ (this). find ("GOODSCOUNT"). text ();
NewRow = myTable. insertRow ();
OCell = newRow. insertCell ();
OCell. setAttribute ("height", "25 ");
OCell. innerHTML = pid;
OCell = newRow. insertCell ();
OCell. innerHTML = "" + pname + "";
OCell = newRow. insertCell ();
OCell. innerHTML =""
OCell = newRow. insertCell ();
OCell. innerHTML = pprice;
});
});
}
},
Error: function (){
Hide ();
Dialog. popTip ("server busy", 2 );
}
});


JS version

The Code is as follows:


If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Hide ();
Var xml = xmlHttp. responseXML;
If (xml = null ){
Dialog. popTip ("the order to be merged cannot be found", 2 );
} Else {
Var myTable = document. getElementById ("t_product ");
Var orders = xml. getElementsByTagName ("ORDER ");
For (var I = 0; I Var order = orders [I];
Var id = order. getElementsByTagName ("ORDERID") [0]. childNodes [0]. nodeValue;
Var status = order. getElementsByTagName ("STATUS") [0]. childNodes [0]. nodeValue;
Alert (status );
If (status = "1 "){
Status = "unconfirmed ";
} Else {
Status = "Confirmed ";
}
Var newRow = myTable. insertRow ();
Var oCell = newRow. insertCell ();
OCell. setAttribute ("height", "25 ");
OCell. innerHTML = "";
OCell = newRow. insertCell ();
OCell. innerHTML = "* The status of the order "+ id +" is: "+ status +"The product information is as follows: ";
OCell = newRow. insertCell ();
OCell. innerHTML = "";
OCell = newRow. insertCell ();
OCell. innerHTML = "";

Var products = order. getElementsByTagName ("PRODUCT ");
For (var I = 0; I Var product = products [I];
Var pid = product. getElementsByTagName ("PRODUCTID") [0]. childNodes [0]. nodeValue;
Var pname = product. getElementsByTagName ("PRODUCTNAME") [0]. childNodes [0]. nodeValue;
Var purl = product. getElementsByTagName ("PRODUCTURL") [0]. childNodes [0]. nodeValue;
Var pprice = product. getElementsByTagName ("PRICE") [0]. childNodes [0]. nodeValue;
Var pcount = product. getElementsByTagName ("GOODSCOUNT") [0]. childNodes [0]. nodeValue;
NewRow = myTable. insertRow ();
OCell = newRow. insertCell ();
OCell. setAttribute ("height", "25 ");
OCell. innerHTML = pid;
OCell = newRow. insertCell ();
OCell. innerHTML = "" + pname + "";
OCell = newRow. insertCell ();
OCell. innerHTML =" "
OCell = newRow. insertCell ();
OCell. innerHTML = pprice;
}
}
}
}
}

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.