本文实例讲述了JS实现读取xml内容并输出到div中的方法。分享给大家供大家参考,具体如下:
note.xml文件结构:
& lt; nooo> & lt; note> & lt; to> George & lt; from> John & lt; heading> Reminder & lt; body>别忘了会议! & lt;/body> & lt;/note> & lt; note> & lt; to> a & lt; from> John & lt; heading> Reminder & lt; body>别忘了会议! & lt;/body> & lt;/note> & lt; note> & lt; to> George & lt; from> John & lt; heading> Reminder & lt; body>别忘了会议! & lt;/body> & lt;/note> & lt;/nooo> >之前利用js将xml输出到div中:
& lt; !DOCTYPE html> & lt; html> & lt; head> & lt;元charset=皍tf - 8”比; & lt; title> www.jb51.net js读取xml & lt; style> .aaaa{宽度:30%;高度:50 px;行高:50 px; text-align:中心;边界:1 px固体darkblue;浮动:左;} & lt;/style> & lt;/head> & lt; body> & lt; div id=" xmlid "祝辞& lt;/div> & lt; script> xmltext=新的XMLHttpRequest; xmltext.open(“获得”、“note.xml”,假); xmltext.send (); 一个=xmltext.responseXML;//. getelementbyid (“xmlid”)。innerHTML=a.getElementsByTagName (“”) [2] childnodes [0] .nodeValue; x=a.getElementsByTagName(“注意”); (i=0; i< x.length;我+ +) { document . write (“& lt; div类=癮aaa”在“); document . write (x[我].getElementsByTagName (“”) [0] childnodes [0] .nodeValue); document . write (“& lt;/div>”); document . write (“& lt; div类=癮aaa”在“); document . write (x[我].getElementsByTagName(“标题”)[0]childnodes [0] .nodeValue); document . write (“& lt;/div>”); document . write (“& lt; div类=癮aaa”在“); document . write (x[我].getElementsByTagName(“身体”)[0]childnodes [0] .nodeValue); document . write (“& lt;/div>”); } & lt;/script> & lt;/body> & lt;/html> >之前运行效果:
/JSON互相转换工具:
强> http://tools.jb51.net/code/xmljson/在线压缩:
http://tools.jb51.net/code/xml_format_compress
强> http://tools.jb51.net/code/xmlformathttp://tools.jb51.net/code/xmlcodeformat
更多关于JavaScript相关内容可查看本站专题:《JavaScript操作XML文件技巧总结》,《JavaScript文件与目录操作技巧汇总》、《JavaScript ajax中操作技巧总结》,《JavaScript错误与调试技巧总结》及《JavaScript数据结构与算法技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。
JS实现读取xml内容并输出到div中的方法示例