473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My Javascript wont read my xml

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>

Jun 12 '06 #1
2 2079
ca******@gmail.com wrote:
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.
--
Rob
Jun 12 '06 #2


ca******@gmail.com wrote:
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>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1940
by: Plamen Valtchev | last post by:
This is my problem: From JavaScript I want to find the list of all defined/loaded JavaScript functions/objects/names within the current scope (html page in a browser). the page could contain...
6
2716
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
23
5054
by: Han | last post by:
Our app runs on end-users machines (apache2.x + php5). At this moment it is quite easy for someone (who has access to the console) to insert a couple lines of php code to steal sensitive info. ...
5
1908
by: manokumar | last post by:
hiye, i notice that some if not all of my folders in winxp pro. are set as read only and its giving me some problem with development. so as the natural thing, i unchecked the read only option and...
3
1528
by: | last post by:
guys i have a asp.net form with a ton of dropdown boxes and i need to accomplish two things on the client with no post back to server. im also thinking that my answer lies in using javascript, but i...
1
1187
by: Ganesh Walavalkar | last post by:
Hello Please read the new concept about JavaScript that I have put up on http://wganesh.blogspot.com/ Here is quick summery of the idea that I have put up: (This is for lazy folks) 1....
25
2552
by: acemtp | last post by:
Hello, I would like to show you a new collaborative website for javascript programmer. It's quite hard to know if some specific code you wrote will work on all browsers, and if not, why it...
6
1601
by: Michael Landberg | last post by:
Hi I am crewating a website using HTML and Javascript. The website can be seen by everybody that has internet. Do I have to consider that some people don't have javascript enabled in their...
8
6142
by: rajesh | last post by:
< script language = javascript c =...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.