Home >Web Front-end >JS Tutorial >JavaScript parsing and reading XML document example code_javascript skills

JavaScript parsing and reading XML document example code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:50:241119browse

JavaScript parsing and reading XML files mainly involves loading and parsing the XML files, and then you can test the contents of the parsed XML files and print them out.
Online demo: http://demo.jb51.net/js/2012/readxml/
Note: When testing, you need to test on the website, iis or apache, be careful not to local Double-click to run the test
index.htm

Copy the code The code is as follows:



Script Home







xml file
Copy code The code is as follows:





Backend homepage
Test page




Backend homepage< ;/MenuItem>
Test page




TABLE is not used because the display is upside down, so I set a variable and then display it! Worth borrowing in the future!

The following is another example:
Written a JavaScript class to read data from an XML file. The implementation code is as follows:
Copy code The code is as follows:

<script> <br>/**<br>* @author Shirdrn <br>*/ <br>function XMLDoc(){}; // Define an XMLDoc class <br>XMLDoc.prototype.xmlFile = ""; / / xmlFile is a member of XMLDoc, which refers to the ".xml" file <br>XMLDoc.prototype.parseXMLDoc = function(){ // Load the member method for parsing XML files <br>var docParser; <br>if(window. ActiveXObject) { // IE browser supports <br>docParser = new ActiveXObject("Microsoft.XMLDOM"); <br>docParser.async = "false"; <br>docParser.load(this.xmlFile); <br> return docParser; <br>} <br>else if(window.DOMParser) { // Mozillia browser supports <br>docParser = new DOMParser() <br>return docParser.parseFromString(this.xmlFile,"text/xml" ); <br>} <br>else { // If it is not IE and Mozillia browsers, it cannot be parsed and returns false. <br>return false; <br>} <br>} <br>XMLDoc.prototype.print = function(readTagName,readTagCnt) { // Print out the content information of the read XML file <br>var xmlDoc = this. parseXMLDoc(); // Call the member method parseXMLDoc() to load the parsed XML file <br>var users = xmlDoc.getElementsByTagName(readTagName); // Get an array of data with the specified tag name users <br>for(var i=0 ; i<users.length ; i ) { // Double loop iteration output<BR>document.write("<B>th" (i 1) "record information: </B><BR>") ; <br>for(var j=0 ; j<readTagCnt ; j ) { <br>var tagname = users[i].childNodes[j].tagName; <br>var textvalue = users[i].childNodes[j ].text; <br>document.write(tagname " = " textvalue ".<BR>"); <br>} <br>} <br>} <br><br>var xmlDoc = new XMLDoc() ; // Create an XMLDoc IDE object instance <br>xmlDoc.xmlFile = "user.xml"; // Set the data of the member variables of the object instance <br>xmlDoc.print("user",6); // Print Output <br></script>

Among them, the content of the XML file user.xml we used for testing is as follows:
Copy the code The code is as follows:


-
-
22240319830000
Shirdrn
26

shirdrn@hotmail.com
13843140000

- 22040319860001
Linda
23
Female
linda@hotmail.com
13843140002



Run the test program and the parsing result output is as follows:
Copy the code The code is as follows:

The first record information:
id = 22240319830000.
name = Shirdrn.
age = 26.
gender = male.
email = shirdrn@hotmail.com.
phone = 13843140000.
The second record information:
id = 22040319860001.
name = Linda.
age = 23.
gender = female.
email = linda@hotmail. com.
phone = 13843140002.

When parsing XML files, make sure to provide support for different types of browsers, mainly IE and Mozillia browsers, otherwise it may not be parsed.
For other instructions, please refer to the comments in the program.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn