JS實現(xiàn)讀取xml內(nèi)容并輸出到div中的方法示例
本文實例講述了JS實現(xiàn)讀取xml內(nèi)容并輸出到div中的方法。分享給大家供大家參考,具體如下:
note.xml文件結(jié)構(gòu):
<nooo> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> <note> <to>a</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> </nooo>
利用js將xml輸出到div中:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>chabaoo.cn js讀取xml</title> <style> .aaaa{width: 30%;height: 50px;line-height: 50px;text-align: center;border: 1px solid darkblue;float: left;} </style> </head> <body> <div id="xmlid"></div> <script> xmltext = new XMLHttpRequest; xmltext.open("GET","note.xml",false); xmltext.send(); a = xmltext.responseXML; //document.getElementById("xmlid").innerHTML = a.getElementsByTagName("to")[2].childNodes[0].nodeValue; x = a.getElementsByTagName("note"); for(i=0;i<x.length;i++) { document.write("<div class='aaaa'>"); document.write(x[i].getElementsByTagName("to")[0].childNodes[0].nodeValue); document.write("</div>"); document.write("<div class='aaaa'>"); document.write(x[i].getElementsByTagName("heading")[0].childNodes[0].nodeValue); document.write("</div>"); document.write("<div class='aaaa'>"); document.write(x[i].getElementsByTagName("body")[0].childNodes[0].nodeValue); document.write("</div>"); } </script> </body> </html>
運(yùn)行效果:
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作XML文件技巧總結(jié)》、《JavaScript文件與目錄操作技巧匯總》、《JavaScript中ajax操作技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
- javascript 讀取XML數(shù)據(jù),在頁面中展現(xiàn)、編輯、保存的實現(xiàn)
- JavaScript 解析讀取XML文檔 實例代碼
- JS讀取XML文件示例代碼
- firefox中JS讀取XML文件
- JS通過ajax動態(tài)讀取xml文件內(nèi)容的方法
- JS實現(xiàn)加載和讀取XML文件的方法詳解
- Jsp結(jié)合XML+XSLT將輸出轉(zhuǎn)換為Html格式
- JS讀取XML文件數(shù)據(jù)并以table形式顯示數(shù)據(jù)的方法(兼容IE與火狐)
- JS使用ajax從xml文件動態(tài)獲取數(shù)據(jù)顯示的方法
- javascript XML數(shù)據(jù)顯示為HTML一例
- JavaScript實現(xiàn)讀取與輸出XML文件數(shù)據(jù)的方法示例
相關(guān)文章
JS在TextArea光標(biāo)位置插入文字并實現(xiàn)移動光標(biāo)到文字末尾
JS在TextArea光標(biāo)位置插入文字+移動光標(biāo)到文字末尾,F(xiàn)irefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd屬性,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06基于JavaScript實現(xiàn)文本一鍵復(fù)制和長按復(fù)制功能
本文主要內(nèi)容分三部分,第一部分是需求分析,第二部分是實現(xiàn)步驟,第三部分是問題詳解,如果您只需要解決問題,請閱讀第一、二部分即可,如果您有更多時間,進(jìn)一步學(xué)習(xí)問題相關(guān)知識點,請閱讀至第三部分2023-10-10用js實現(xiàn)隨機(jī)返回數(shù)組的一個元素
js實現(xiàn)隨機(jī)返回數(shù)組的一個元素,這是個奇妙的方法。適合做標(biāo)題性質(zhì)文字的隨機(jī)輪換顯示2007-08-08javascript 控制 html元素 顯示/隱藏實現(xiàn)代碼
javascript 控制 html元素 顯示/隱藏,下面是一些逐步的實現(xiàn)原理。2009-09-09