使用jquery解析XML範例程式碼,jqueryxml範例程式碼

來源:互聯網
上載者:User

使用jquery解析XML範例程式碼,jqueryxml範例程式碼

xml檔案結構:books.xml

<?xml version="1.0" encoding="UTF-8"?><root><book id="1"><name>深入淺出extjs</name><author>張三</author><price>88</price></book><book id="2"><name>鋒利的jQuery</name><author>李四</author><price>99</price></book><book id="3"><name>深入淺出flex</name><author>王五</author><price>108</price></book><book id="4"><name>java編程思想</name><author>錢七</author><price>128</price></book></root>

頁面代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>jquery解析xml</title><script type="text/javascript" src="js/jquery-1.4.2.min.js"></script><script type="text/javascript">$(function(){$.post('books.xml',function(data){//尋找所有的book節點var s="";$(data).find('book').each(function(i){var id=$(this).attr('id');var name=$(this).children('name').text();var author=$(this).children('author').text();var price=$(this).children('price').text();s+=id+"    "+name+"    "+author+"    "+price+"<br>";});$('#mydiv').html(s);});});</script></head><body><div id='mydiv'></div></body></html>

:


怎使用jquery對xml檔案進行 增 刪 改 操作

你把問題描述清楚啊。

是對WEB頁面的XML資料的進行操作?
是對傳遞來的XML資料進行操作?
是對本地硬碟的XML檔案進行操作?

提供個思路:

通過jquery的$.ajax()請求該xml資源,裡面參數dataType:xml,如此,即可在本地js代碼上遍曆此xml文檔。把檔案顯示於頁面。做了修改,刪除操作後,把資料用JSON方式傳遞到後台,進行檔案讀寫操作。
$.ajax({
url : rootPath + "/userInfo.do?method=saveSetting&columnIds=。。。",
dataType : "json",
success : function(response) {
if (response) {

alert("儲存成功");
}
else
alert("儲存失敗" );
}
}
});

一、xml檔案\內容讀取 1、讀取xml檔案 $.get( xmlfile.xml , function (xml){ //xml即為可以讀取使用的內容,具體讀取見第2點 }); 2、讀取xml內容 如果讀取的xml是來於xml檔案,這結合上面的那點,處理如下 $.get( xmlfile.xml , function (xml){ $(xml). fi
一、xml檔案\內容讀取
1、讀取xml檔案
$.get("xmlfile.xml",function(xml){
//xml即為可以讀取使用的內容,具體讀取見第2點
});
2、讀取xml內容
如果讀取的xml是來於xml檔案,這結合上面的那點,處理如下
$.get("xmlfile.xml",function(xml){
$(xml).find("item").length;
});
如果讀取的是xml字串,則要注意一點,xml字串的必然被”<xml>”和”</xml>”包圍才可以被解析
$("<xml><root><item></item></root></xml>").find("item").length; 二、解析xml內容樣本xml:<?xml version="1.0" encoding="utf-8" ?><fields>
<field Name="Name1">
<fieldname>dsname</fieldname>
<datatype>字元</datatype>
</field>
<field Name="Name2">
<fieldname>dstype</fieldname>
<datatype>字元</datatype&......餘下全文>>
 
JQUERY解析XML,並返回html

var xml="那個xml字串";var table=$("那個table的選取器");$(xml).find("viewentry").each(function(){ var tr=$("<tr>"); $(this).find("entrydata").each(function(){ tr.append("<td>"+$(this).child("text").text+"</td>"); }); table.append(tr);});我沒有測試過,不過大概是這麼個思路

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.