JS load and read XML files

Source: Internet
Author: User
Tags file url

Sometimes in the development of the use of JS to load and read XML files, write down to provide reference, here is mainly in two steps to complete:


1,js load XML file

The steps are generally (1), an XML DOM object is established, (2), the load mode is set, asynchronous (recommended) or synchronized, (3) The XML file URL is provided, and the Load method is called, roughly as follows:

var xmlfilename= "Xxfile.xml";
var xmldoc= ';
if (window. ActiveXObject) {//IE   
	var activexnamelist=new Array ("MSXML2. domdocument.6.0 "," MSXML2. domdocument.5.0 "," MSXML2. domdocument.4.0 "," MSXML2. domdocument.3.0 "," MSXML2. DOMDocument "," Microsoft.XMLDOM "," MSXML. " DOMDocument ");
	for (Var h=0;h<activexnamelist.length;h++)
	{
		try{
			xmldoc=new ActiveXObject (activexnamelist[h));
		} catch (e) {
			continue;
		}
		if (xmldoc) break;	
	}
else if (document.implementation && document.implementation.createDocument) {//non IE
   	xmldoc= Document.implementation.createDocument ("", "", null);	
} else{
	alert (' Can not create XML DOM object, update your browser ... ');
Xmldoc.async=false;  synchronization, to prevent the subsequent program processing when the file has not been loaded to complete the error, so the synchronization of XML files after loading and then do the following processing
xmldoc.load (xmlfilename);//Load XML



2,JS Read XML file node

After loading the XML file is to read the node of the XML file, you can use the corresponding method of DOM, the MS IE other browser reading method similar, for example:

For example, the following XML file structure: 
<visiter>
	<area areaid= "Shenzhen" >
		<areaname>shenzhen</areaname >
		<user userid= "001" >			
			<name>shenzhenNBA</name>
			<sex>man</sex>
		</user>
	</area>
	<area areaid= "Shanghai" >
		<areaname>shenzhen</areaname >
		<user userid= "002" >			
			<name>xiaoming</name>
			<sex>woman</sex>
		</user>
		<user userid= "003" >			
			<name>zhangsan</name>
			<sex>man </sex>
		</user>
	</area>
</visiter>

JS reads the area nodes in the XML file in the following way:
var nodelist= xmlDoc.documentElement.getElementsByTagName ("area");//IE for
(var i=0;i<nodelist.length;i++) {
	//... Traversal operation ...
}

var nodelist=xmldoc.getelementsbytagname ("area");  Non ie for
(var i=0;i<nodelist.length;i++) {
	//... Traversal operation ...
}


There are also some ways to read nodes:

MS IE
Node.text; Reading text values for node nodes
Node.childnodes[i].text; //Read the text of the first [direct next level] child under node
Node.getattribute ("AttributeName"); property value that reads node node's attribute name is AttributeName
There are other ways to do so, you can search the Internet



Non-MS IE
Node.nodevalue;
Reading text values for node nodes
Node.childnodes[i].nodevalue; Read the text of the first [direct Next level] child node under node
Node.getattribute ("AttributeName"); property value that reads node node's attribute name is AttributeName
There are other ways to do so, you can search the Internet





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.