My Javascript wont read my xml

C

catronro

I want to read an xml file and build a string based on that file. But
when i try to implement it i get an Object Required error. Any help
would be greatly appreciated.

try
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var html = "";
xmlDoc.load('siteMap.xml');
var xmlObj=xmlDoc.documentElement;
alert(xmlObj.xml);

for(var i=0; i < xmlObj.childNodes.length; i++)
{
var path = xmlObj.childNodes(i).getAttribute("address");
var name = xmlObj.childNodes(i).firstChild.text;
html += "<a href='" +path+ "'>"+name+"</a> &nbsp;
&nbsp;";
}
document.write(html);

}
catch(err)
{
alert(err.description);
}

This is the file im trying to read in

<?xml version="1.0" encoding="utf-8" ?>
<mainSiteMap>
<siteMap address="home.htm">HOME</siteMap>
<siteMap address="events.htm">EVENTS</siteMap>
<siteMap address="contacts.htm">CONTACTS</siteMap>
<siteMap address="albums.htm">ALBUMS</siteMap>
<siteMap address="register.htm">REGISTER</siteMap>
</mainSiteMap>

This is how i call the javascript in my HTML file

<script type="text/javascript" src="Code/Javascript.js"></script>
 
R

RobG

I want to read an xml file and build a string based on that file. But
when i try to implement it i get an Object Required error. Any help
would be greatly appreciated.

When do you get the error? Do you get the XML file OK, or does it bomb
before then?

try
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

Do you want this to work in browsers other than those that support
Microsoft's proprietary ActiveX?

var html = "";
xmlDoc.load('siteMap.xml');
var xmlObj=xmlDoc.documentElement;
alert(xmlObj.xml);

What happens here? What does the alert show?

for(var i=0; i < xmlObj.childNodes.length; i++)
{
var path = xmlObj.childNodes(i).getAttribute("address");
var name = xmlObj.childNodes(i).firstChild.text;
html += "<a href='" +path+ "'>"+name+"</a> &nbsp;
&nbsp;";
}
document.write(html);

If you are doing this just to generate a simple menu, it's likely the
worst way of going about it.

}
catch(err)
{
alert(err.description);

What happens here?

}

This is the file im trying to read in

<?xml version="1.0" encoding="utf-8" ?>
<mainSiteMap>
<siteMap address="home.htm">HOME</siteMap>
<siteMap address="events.htm">EVENTS</siteMap>
<siteMap address="contacts.htm">CONTACTS</siteMap>
<siteMap address="albums.htm">ALBUMS</siteMap>
<siteMap address="register.htm">REGISTER</siteMap>
</mainSiteMap>

This is how i call the javascript in my HTML file

<script type="text/javascript" src="Code/Javascript.js"></script>

Why not use the server to generate HTML from the XML and serve that
directly to the client? Using client scripting to munge XML into HTML
for a simple menu is just asking for trouble - you could do it on the
server with XSLT and get a far more reliable result.

If you want to use XMLHttpRequest, use a library like that provided at:

<URL:http://www.ajaxtoolbox.com/>

If you don't want all the functionality, strip out what you don't want.
IE 7 supports XMLHttpRequest (apparently, I haven't tested it),
hopefully ajaxtoolbox will not need any changes in that regard while
still supporting older IE and W3C browsers.
 
M

Martin Honnen

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var html = "";
xmlDoc.load('siteMap.xml');

You need at least
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
if (xmlDoc.load('siteMap.xml')) {
// how you can process/access the DOM here
}
else {
// deal with parse error e.g.
alert(xmlDoc.parseError.reason);
}

In the end however using synchronous requests is not a good idea as it
blocks the complete browser so doing
xmlDoc.async = true;
and processing the XML in an onreadystatechange handler is a better idea.
To go cross browser you might prefer to use XMLHttpRequest, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top