Home > Article > Web Front-end > How to operate JS to read xml content and output it into div
This time I will show you how to operate JS to read xml content and output it into a div. What are the precautions for operating JS to read xml content and output it into a div? The following is a practical case, let's take a look.
note.xml file structure:
<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>
Use js to output xml to p:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>www.jb51.net js读取xml</title> <style> .aaaa{width: 30%;height: 50px;line-height: 50px;text-align: center;border: 1px solid darkblue;float: left;} </style> </head> <body> <p id="xmlid"></p> <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("<p class='aaaa'>"); document.write(x[i].getElementsByTagName("to")[0].childNodes[0].nodeValue); document.write("</p>"); document.write("<p class='aaaa'>"); document.write(x[i].getElementsByTagName("heading")[0].childNodes[0].nodeValue); document.write("</p>"); document.write("<p class='aaaa'>"); document.write(x[i].getElementsByTagName("body")[0].childNodes[0].nodeValue); document.write("</p>"); } </script> </body> </html>
I believe you have mastered the method after reading the case in this article, please come for more exciting information Pay attention to other related articles on php Chinese website!
Recommended reading:
Use jquery to get the specific content of the uploaded file
JS to make countdown recovery click (forced reading)
The above is the detailed content of How to operate JS to read xml content and output it into div. For more information, please follow other related articles on the PHP Chinese website!