Important alert: (current site time 7/15/2013 12:45:08 PM EDT)
 

VB icon

Cross Browser XmlHelper

Email
Submitted on: 6/15/2004 11:54:54 PM
By: Lewis E. Moten III  
Level: Intermediate
User Rating: Unrated
Compatibility: JavaScript
Views: 5978
author picture
(About the author)
 
     Allows you to easily load xml files in MSIE, Netscape, and Mozilla. Read nodes with xpath, select attribute values and text - all with the same interfaces.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Cross Browser XmlHelper
' Description:Allows you to easily load xml files in MSIE, Netscape, and Mozilla. Read nodes with xpath, select attribute values and text - all with the same interfaces.
' By: Lewis E. Moten III
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4263&lngWId=14'for details.'**************************************

/*
 * Cross Browser Xml Helper Class
 * Author: Lewis Moten
 * Date: June 15, 2004 11:36 PM EST
 * Url: http://www.lewismoten.com
 *
 * Tested with MSIE 6.0, Netscape 7.1, Mozilla 1.6
 */
var XmlActiveX = new Boolean(false); // Determines if xml was created with ActiveX
var Xml = null; // Current Xml document
// Event called after xml file loads. Searches for
// a method that YOU create called Xml_Loaded to handle
// what goes on after the xml file loads.
function LoadXml_Loaded()
{
	if(Xml_Loaded){Xml_Loaded();}
}
// load an Xml file Asynchronously
function LoadXml(fileName)
{
	if(document.implementation && document.implementation.createDocument)
	{
		XmlActiveX = false;
		Xml = document.implementation.createDocument("", "doc", null);
		Xml.onload = LoadXml_Loaded;
		Xml.load(fileName);
		return;
	}
	XmlActiveX = true;
	try{Xml = new ActiveXObject("Msxml2.DOMDocument");XmlActiveX = true;}
	catch (e){Xml = new ActiveXObject("Msxml.DOMDocument");XmlActiveX = true;}
	if(Xml == null) return;
	Xml.async = false;
	Xml.load(fileName);
	LoadXml_Loaded();
}
// select a collection of nodes that match xpath specified
function XmlSelectNodes(xpath)
{
	if(XmlActiveX) return Xml.selectNodes(xpath);
	var XPathResult = Xml.evaluate(xpath, Xml, null, 5, null);
	var Nodes = new Array();
	while(node = XPathResult.iterateNext())
		Nodes[Nodes.length] = node;
	return Nodes;
}
// Select attribute value of the node
function XmlNodeAttribute(node, name)
{
	if(XmlActiveX) return node.getAttribute(name);
	if(node.attributes)
		if(node.attributes[name])
			if(node.attributes[name].nodeValue) return node.attributes[name].nodeValue;
}
// Select text value of the node
function XmlNodeText(node)
{
	if(XmlActiveX) return node.text;
	if(node.firstChild)
		return node.firstChild.nodeValue;
	return null;
}


Other 15 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
7/9/2004 4:08:41 PM

You have no examples or documentation on this codes usage. I consider it below without those things. If it is worth writing the code, it is worth writing an example and other documentation. The code runs a bit buggy in my browser too, not really cross browser.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.