Google Answers Logo
View Question
 
Q: Loading XML file via javascript ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Loading XML file via javascript
Category: Computers > Programming
Asked by: iron_jon-ga
List Price: $200.00
Posted: 30 Mar 2005 07:17 PST
Expires: 29 Apr 2005 08:17 PDT
Question ID: 502623
I am writing a web application which requires the loading and parsing
of an XML file.  The application only needs to run on Internet
Explorer v.6+  I use an ActiveXObject for the document object and the
load method, i.e.:

	xDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");	
	xDoc.load( escape( xmlFile ) );

The loading and consequent parsing WORKS but ONLY if there is user
interaction, specifically an alert box or debugger session, which is
not acceptable.  To be more specific, the xDoc object above will not
reach readyState '4' unless there is an alert box.  Currently, i test
if the document has loaded and if not (which is doesn't) i
(essentially) say:

if (xDoc.readyState < 4){
   alert("The document has loaded.");
}

I can then parse the document.  This makes no sense, but it works.

I've tried using xDoc.onreadystatechange event trigger, but the
function is not called with readyState '4' unless i'm in a debugger
session or i use the alert box trick.

I haven't found this problem documented anywhere and I have wasted a
lot of time on it, so if someone has faced and conquered this
situation, please help!

I am serving the same javascript app from both a windows and a solaris
server.  Alas, I access them both via VPN so I cannot point you to the
servers serving the files.  I am running WindowsXP w/SP2, IE
6.0.2900... but the problem exists with other OS versions.  The
problem DOES NOT EXIST if i run the app locally, only from a remote
server.

Request for Question Clarification by webadept-ga on 30 Mar 2005 11:25 PST
Hi, 

I think I have this worked out for you. Could you post here a short
example (or long.. doesn't really matter, just thinking of the page
space) of your current xml file so I can insure that this is going to
work for you?

thanks, 

webadept-ga

Clarification of Question by iron_jon-ga on 30 Mar 2005 13:22 PST
The entire file is huge so I'll include as small of a chunk as makes
sense but broad enough to be useful:


// loadXML() is called by an init function which is called by onLoad

function loadXML() {
	//loads the XML file and calls generateObjects to parse it
	loadFile( xmlFile );
	while( xDoc.readyState < 4 ){
		//egregious hack to force the browser to wait before further execution
		alert("Ready to Create Presentations!");
		generateObjects();	
	}		
	if( !xDoc.parsed ){
		alert("couldn't load xml file.");
	}
}


function loadFile( xFile ){
	//initializes the ActiveXObjects for loading XML
	var moz = document.implementation && document.implementation.createDocument;
	var ie = window.ActiveXObject;
	
	if (moz){
		alert("Your browser is not yet supported by StoryBuilder 3.0");
		return false;
	} else if (ie){
		xDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
		xChunk = new ActiveXObject("Msxml2.DOMDocument.3.0");
	} else {
		alert ("Your browser does not support features required for StoryBuilder 3.0");
		return false;
	}

	return xDoc.load( escape( xFile ) );
}

Request for Question Clarification by webadept-ga on 31 Mar 2005 08:28 PST
Hi Iron Jon ... sorry for not making myself clear here.. I just needed
a sample of the XML file you are loading so I can test the loading..
not your javascript code.. though it is nice to have that too.

webadept-ga

Clarification of Question by iron_jon-ga on 31 Mar 2005 13:09 PST
Ah yes - i didn't read that clearly.  Here you go:

<?xml version="1.0" encoding="UTF-8"?>
<StoryBuilder>
	<category name="Audience">
		<entry name="all">
			<file id="6"/>
			<file id="4"/>
			<file id="9"/>
			<file id="11"/>
			<file id="12"/>
		</entry>
		<entry name="business">
			<file id="1"/>
			<file id="7"/>
			<file id="5"/>
		</entry>
		<entry name="technical">
			<file id="3"/>
			<file id="2"/>
		</entry>
	</category>
	<category name="Portfolio">
		<entry name="all">
			<file id="6"/>
			<file id="8"/>
			<file id="9"/>
			<file id="11"/>
			<file id="12"/>
		</entry>
		<entry name="HAN">
			<file id="1"/>
			<file id="2"/>
		</entry>
		<entry name="LAN switching">
			<file id="4"/>
		</entry>
		<entry name="routing: enterprise">
			<file id="7"/>
		</entry>
		<entry name="security">
			<file id="4"/>
			<file id="7"/>
			<file id="5"/>
		</entry>
		<entry name="storage">
			<file id="4"/>
			<file id="1"/>
			<file id="3"/>
			<file id="7"/>
			<file id="2"/>
		</entry>
		<entry name="wireless/mobility">
			<file id="5"/>
		</entry>
	</category>
	<category name="Segment">
		<entry name="all">
			<solution name="all">
				<file id="6"/>
				<file id="8"/>
			</solution>
		</entry>
		<entry name="commercial">
			<vertical name="all">
				<file id="2"/>
				<file id="9"/>
				<file id="11"/>
				<file id="12"/>
			</vertical>
			<vertical name="real estate/construc">
				<file id="5"/>
			</vertical>
			<solution name="all">
				<file id="6"/>
			</solution>
			<solution name="WAN">
				<file id="5"/>
			</solution>
		</entry>
		<entry name="enterprise">
			<vertical name="all">
				<file id="6"/>
				<file id="1"/>
				<file id="3"/>
				<file id="7"/>
				<file id="2"/>
				<file id="9"/>
				<file id="11"/>
				<file id="12"/>
			</vertical>
			<solution name="all">
				<file id="6"/>
				<file id="8"/>
			</solution>
			<solution name="IOS">
				<file id="1"/>
				<file id="2"/>
			</solution>
			<solution name="WAN">
				<file id="5"/>
			</solution>
			<solution name="datacenter">
				<file id="4"/>
				<file id="1"/>
				<file id="7"/>
				<file id="2"/>
			</solution>
		</entry>
		<entry name="service provider">
			<vertical name="all">
				<file id="6"/>
				<file id="8"/>
				<file id="1"/>
				<file id="3"/>
				<file id="7"/>
				<file id="2"/>
			</vertical>
			<vertical name="cable">
				<file id="4"/>
			</vertical>
			<solution name="all">
				<file id="6"/>
				<file id="8"/>
			</solution>
			<solution name="DCN">
				<file id="3"/>
			</solution>
			<solution name="IOS">
				<file id="1"/>
				<file id="2"/>
			</solution>
			<solution name="managed svcs for SMBs">
				<file id="3"/>
			</solution>
		</entry>
	</category>
	<category name="Theater">
		<entry name="all">
			<file id="6"/>
			<file id="8"/>
			<file id="4"/>
			<file id="1"/>
			<file id="3"/>
			<file id="7"/>
			<file id="2"/>
			<file id="5"/>
				<file id="9"/>
				<file id="11"/>
				<file id="12"/>
		</entry>
	</category>
	<category name="Type">
		<entry name="all">
		</entry>
		<entry name="body">
			<file id="4"/>
			<file id="1"/>
			<file id="3"/>
			<file id="7"/>
			<file id="2"/>
			<file id="5"/>
		</entry>
		<entry name="full">
			<file id="9"/>
			<file id="10"/>
			<file id="11"/>
			<file id="12"/>
		</entry>
		<entry name="case study">
			<file id="7"/>
		</entry>
		<entry name="intro">
			<file id="6"/>
			<file id="8"/>
		</entry>
		<entry name="conclusion">
			<file id="6"/>
			<file id="8"/>
		</entry>
	</category>
	<files>
		<file>
			<file_id>1</file_id>
			<display_name>Enterprise Storage for Business</display_name>
			<author>sredmore</author>
			<create_date>2005-01-01 00:00:00</create_date>
			<description>This is the description for the enterprise business
storage chunk that applies to multiple solutions</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/business</location>
		</file>
		<file>
			<file_id>2</file_id>
			<display_name>Enterprise Storage for Technologists</display_name>
			<author>sredmore</author>
			<create_date>2005-01-01 00:00:00</create_date>
			<description>This is the description for the enterprise business
storage chunk that applies to multiple solutions</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/technical</location>
		</file>
		<file>
			<file_id>3</file_id>
			<display_name>Service Provider Storage for Technologists</display_name>
			<author>sredmore</author>
			<create_date>2005-01-01 00:00:00</create_date>
			<description>This is the description for the service provider tech
storage chunk that applies to multiple solutions</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/technical</location>
		</file>
		<file>
			<file_id>4</file_id>
			<display_name>Service Provider Data Center stuff</display_name>
			<author>sredmore</author>
			<create_date>2005-01-01 00:00:00</create_date>
			<description>This is the description for the service provider data
center chunk that applies to multiple portfolios</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>5</file_id>
			<display_name>Commerical VPN stuff for real estate</display_name>
			<author>sredmore</author>
			<create_date>2005-01-01 00:00:00</create_date>
			<description>This is the description for the commercial chunk that
applies to real estate</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>6</file_id>
			<display_name>Generic Intro</display_name>
			<author>sredmore</author>
			<create_date>2005-01-02 00:00:00</create_date>
			<description>This is a generic intro</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>7</file_id>
			<display_name>We can Build It</display_name>
			<author>sredmore</author>
			<create_date>2005-01-20 00:00:00</create_date>
			<description>How Cisco can help you build your data center with our
army of Data Center Professionals</description>
			<services>1</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>8</file_id>
			<display_name>Generic Conclusion</display_name>
			<author>sredmore</author>
			<create_date>2005-01-20 00:00:00</create_date>
			<description>This is a generic conclusion</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>9</file_id>
			<display_name>Really Awesome Presentation</display_name>
			<author>sredmore</author>
			<create_date>2005-02-14 00:00:00</create_date>
			<description>This is the best presentation ever</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>10</file_id>
			<display_name>Services Preso</display_name>
			<author>sredmore</author>
			<create_date>2005-01-20 00:00:00</create_date>
			<description>Everything services has to know</description>
			<services>1</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>11</file_id>
			<display_name>Sales Is Us</display_name>
			<author>sredmore</author>
			<create_date>2005-01-20 00:00:00</create_date>
			<description>What you want, baby we got it</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
		<file>
			<file_id>12</file_id>
			<display_name>Iinferno</display_name>
			<author>sredmore</author>
			<create_date>2005-01-20 00:00:00</create_date>
			<description>Welcome to the 7th Circle.  Hot hot hot!</description>
			<services>0</services>
			<location>/opt/httpd/root-wwwin/htdocs/marketing/ptmo/sb3/presos/sample</location>
		</file>
	</files>
</StoryBuilder>

Request for Question Clarification by webadept-ga on 31 Mar 2005 16:48 PST
Great, that should do it.. I'll run some tests and see if this is
going to work out for you and get back to you shortly.

thanks,

webadept-ga
Answer  
Subject: Re: Loading XML file via javascript
Answered By: webadept-ga on 31 Mar 2005 18:18 PST
Rated:5 out of 5 stars
 
Hey Jon, 

Hmmm... this is a bit odd isn't it, and I can see that if what I'm
running across here is what you are running across there, you should
be just about as bald as I am at the moment.

I put together what I thought was a fix, but before I do anything
really I like to test with "known good"'s .. little habit I picked up
from my sys admin days.

So I got your data, and tested it as a local file, with your data..
worked just fine. Loaded it on my remote webserver, tested it, worked
just fine, and then I started to work with it on my laptop ...
thinking everything was "just fine". My laptop runs a local server
using Apache, so I loaded it into my htdocs directory, typed in my
local URL and turned to get my coffee. When I turned back, I had a
blank page. <minor eek>

What I believe is happening here (though I don't really know why at
the moment) is that for some reason, a local machine can't run this on
its own, which is weird. To test this I setup my desktop (it was still
in a box from a move) and tried to get it from there... still a blank
page, and then I changed the HOST file to see if that would be a quick
fix and still a blank page.

So, Looks like local network setup is the problem here on my end. What
I need from you at this point, before I start down this path, is to
verify: this is the same problem you are having. I noticed that you
mentioned above that you only have the problem from the remote sever.

First, go to http://www.webadept.net/test.html and verify that the XML
loads for you on your computer.

If you can load the file, then load both the test.html you found there
and the test.xml on to your remote server and see if that works. If it
does, great, we'll get you going using that code and I'll work out why
my local server isn't bringing it up on my own. If not, then my local
server might have something in common with your remote server and we
can go at it from there.

If you have problems loading the test.html on your remote server, then let me know:
a) the server type you are using (Apache, IIS, etc I see what you have
up there but you don't mention the server type, just the OS)
b) is this server something you can adjust settings on
c) what version is the remote server

Thanks, and look forward to getting this worked out for you. 

webadept-ga

Clarification of Answer by webadept-ga on 31 Mar 2005 18:39 PST
Ah.. :-) 

I just figured out what is up with my setup here. I code a great deal
of PHP, and just for my local development I put in my apache
httpd.conf file this :

<IfModule mod_mime.c>
	AddType application/xml .xml
	AddType application/x-httpd-php .php .html
	AddType application/x-httpd-php-source .phps
</IfModule>

And it wasn't loading the XML. Removing the .html from the PHP
application line solved the problem.

check that and see if it works for you as well. 

webadept-ga

Clarification of Answer by webadept-ga on 31 Mar 2005 18:41 PST
Also check to see that your .html is not an extension for any of the
application lines. It appears that any parser running through an .html
document will stop an external XML file from loading properly.

webadept-ga
iron_jon-ga rated this answer:5 out of 5 stars
The reasearcher answered my question with a code example that was
right on the money.  The actual bit of info I needed, the async
property on the XMLDocument object was uncommented but it was easy
enough to spot and implement.  Thanks a ton!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy