Jump to content
Wikibooks The Free Textbook Project

JavaScript/Handling XML

From Wikibooks, open books for an open world



Simple function to open an XML file

[edit | edit source ]

This function first tries for Microsoft Internet Explorer, then for Firefox and others:

functionloadXMLDoc(xmlfilename){
varevent=newError();
// Internet Explorer
try{
varxmlDoc=newActiveXObject("Microsoft.XMLDOM");
}catch(event){
// Firefox, Mozilla, Opera, others
try{
xmlDoc=document.implementation.createDocument("","",null);
}catch(event){
throw(event.message);
}
}
try{
xmlDoc.async=false;
xmlDoc.load(xmlfilename);
return(xmlDoc);
}catch(event){
throw(event.message);
}
return(null);
}

Usage

[edit | edit source ]
varobjXML=loadXMLDoc("filename.xml");
varoNodes=objXML.getElementsByTagName("AnyTagYouWish");

Now you can do any DOM operations on oNodes.

XML modifications can't be saved in JavaScript, as this is clientside...

AltStyle によって変換されたページ (->オリジナル) /