We help IT Professionals succeed at work.

Load XML File with JAvascript Clientside

StrongD1
StrongD1 asked
on
435 Views
Last Modified: 2013-11-11
I need to load an XML file from the clients C: drive.

The path is C:\ERSXML\IQ.XML


So when the user clicks an HTML button in the browser, it will look to the directory to load the file...

I posted the code I have, keep getting errors...  is there a trick to loading files off of user C: drive?

This function loads the XML on the C:\ counts the elements in the XML file...

Please help...  I am not good yet at JavaScript...  
<head>
<title>Untitled</title>
</head>
<script type="text/javascript">
function parseXML() {
var menu;
menu = new ActiveXObject("MSXML.DOMDocument");
menu.load("C:\ERSXML\IQ.xml");
var testNode;
testNode = menu.documentElement.firstChild;
var findNodes;
findNodes2 = testNode.getElementsByTagName("*");
findNodesCount2 = findNodes2.length;
document.write(findNodesCount2 + "<br>");
document.write(findNodes2.item(2).nodeName + "<br>")
document.write("<hr>");
var i;
for (i = 0; i < findNodesCount2; i++) {
document.write(findNodes2.item(i).nodeName + " - ");
document.write(findNodes2.item(i).firstChild.nodeValue + "<br>");
}
 
</script>
<body>
<button type="" onclick="parseXML()"></button>
</body>
</html>

Open in new window

Comment
Watch Question

Author

Commented:
Here is what the XML file looks like... it is created on the server side using .NET

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <question_text>Stationary Source Correct </question_text>
  </Table>
  <Table>
    <question_text>Building Number Correct</question_text>
  </Table>
  <Table>
    <question_text>Permit Equipment Description Verified   Check serial number and manufacturer number</question_text>
  </Table>
  <Table>
    <question_text>Opacity Reading Required</question_text>
  </Table>
  <Table>
    <question_text>Operator Training Adequate for Permit Compliance</question_text>
  </Table>
  <Table>
    <question_text>a. Manager/Alternate Appointment letter current</question_text>
  </Table>
  <Table>
    <question_text>Equipment Operational  Check permit for requirements that may be needed such as:</question_text>
  </Table>
  <Table>
    <question_text>a. Automatic fuel oil shutoff system for flameout in good condition</question_text>
  </Table>
  <Table>
    <question_text>b. Firebox and convection section is gas-tight</question_text>
  </Table>
  <Table>
    <question_text>c. Fuel flow meters installed on each unit</question_text>
  </Table>
  <Table>
    <question_text>Equipment affecting air quality (contaminating or eliminating) shall be permitted. a. Permit shall be affixed to the equipment or maintained readily available at all times on the operating premises</question_text>
  </Table>
  <Table>
    <question_text>Pass No. 1/4 Ringelmann Test (5% opacity) after engine achieves normal operating temperature</question_text>
  </Table>
</NewDataSet>

Commented:
First - your javascript is missing a closing brace "}" for your function.

Next, when you call document.write after the page has load, it is actually overwriting the existing document and the javascript that is running!

Commented:
you also have errors with the way you are trying to traverse the document. if you can tell me what you are trying to acheive, i can help you with that.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks alien109,

Have question, on the menu.load("test.xml");  

How do I tell it the c: drive?   IE do I just say menu.load("c:\ERSXML\IQ.xml");

IQ.xml  is the name of the file...  it is located on the users pc c:\ERSXML\

Author

Commented:
Never mind alien109.... I did it with  menu.load("C:/ERSXML/IQ.xml");

This works excellent... thanks for your help!!!!

I need a good javascript 101 book, can you suggest any?

Author

Commented:
THanks again...  please let me know about a book if you know of any...

Thanks

Author

Commented:
Alien109

I do have another question, if you want me to submit another question I will... I guess I should have told you what I wanted to do..

I want to take that XML file that will be on the users C: drive, load it into memory... hold it there...

There will be a HTML text box ... each element will be loaded one at a time into the text box...  the user will have forward and backward arrows...  so if the user click forward, the next element will be loaded into the text box... if these choice the back arrow, it previous element (Or question) will be load into the text box.
I have done this will .NET on the server side, but it is slow and that is why I would rather do it on client side.  Plus it doens't make sense to do it server side.

Once the load the XML elements into an array, will it stay there?  So if I have another JavaScript function for the forward and backward buttons to load the next or previous element, it will work?

Commented:
I can't recommend any books to be honest.

Providing you don't navigate away from the page, the defined variables will still be availble (within scope of course). You can either define a global var, or just pass the result to another function.

There shouldn't be any problem with what you want to do. Here's a quick example. This is a little hacktastic, but should get you started.

Cheers...




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
	<meta http-equiv="Content-Language" content="en-us" />
 
	<title></title>
 
<style type="text/css">
#question {
	border:1px solid #000;
	padding:20px;
	margin:10px 0;
}
button {
	margin-right:10px;
}
</style>
 
<script type="text/javascript">
 
var i = -1;
var questions = [];
 
function parseXML() {
	var stuff = document.getElementById("stuff");
	var menu;
	menu = new ActiveXObject("MSXML.DOMDocument");
	menu.load("test.xml");
	var objNodes = menu.selectNodes("//NewDataSet/Table/question_text");
	for(var i = 0; i < objNodes.length; i++)
	{
		questions.push(objNodes[i].text);
	}
}
 
function showQuestion(d) {
	i+= d;
	if(i >= questions.length) i = questions.length - 1;
	if(i < 0) i = 0;
	question.innerHTML = questions[i];
}
 
</script>
 
</head>
 
<body onload="parseXML()">
	<button id="prev" type="" onclick="showQuestion(-1)">Previous</button>
	<button id="next" type="" onclick="showQuestion(1)">Next</button>
	<div id="question">Click buttons to navigate through questions</div>
 
</body>
</html>

Open in new window

Author

Commented:
THanks a ton alien109!!!

I appreciate the help...

Author

Commented:
WOw Alien109... you saved me a lot of headache.  

What you gave me is perfect, plus I can learn from your code.  I am good with backend coding, but I suck at frontend web stuff and browser related stuff.  I wish we had someone on our team that was good at JavaScript, HTML, and CSS... but we don't, and I am having to do it all.  

But thanks again, this is great, plus it gives me something to mess with.  This was a great head start.

THanks again.  

Commented:
No worries. Glad to help. :)

Here's a great site to help you with all things web...
http://www.w3schools.com/

Author

Commented:
Thanks again...  I really appreciate all of this.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.