Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load XML File with JAvascript Clientside

Posted on 2009-04-01
13
Medium Priority
?
389 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

0
Comment
Question by:StrongD1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 1

Author Comment

by:StrongD1
ID: 24041833
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>
0
 
LVL 12

Expert Comment

by:alien109
ID: 24042140
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!
0
 
LVL 12

Expert Comment

by:alien109
ID: 24042301
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.
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 12

Accepted Solution

by:
alien109 earned 2000 total points
ID: 24042421
Just do this...

<html>
<head>
<title>Untitled</title>
</head>
<script type="text/javascript">
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");
      stuff.innerHTML += objNodes.length + "<br />";
      stuff.innerHTML += objNodes[2].parentNode.nodeName + "<br />";
      stuff.innerHTML += "<hr />";
      for(var i = 0; i < objNodes.length; i++)
      {
            stuff.innerHTML += objNodes[i].nodeName + " - ";
            stuff.innerHTML += objNodes[i].text;

      }
}
</script>
<body>
<div id="stuff"></div>
<button type="" onclick="parseXML()"></button>
</body>
</html>
0
 
LVL 1

Author Comment

by:StrongD1
ID: 24043078
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\
0
 
LVL 1

Author Comment

by:StrongD1
ID: 24043119
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?

0
 
LVL 1

Author Closing Comment

by:StrongD1
ID: 31565444
THanks again...  please let me know about a book if you know of any...

Thanks
0
 
LVL 1

Author Comment

by:StrongD1
ID: 24043464
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?
0
 
LVL 12

Expert Comment

by:alien109
ID: 24043736
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

0
 
LVL 1

Author Comment

by:StrongD1
ID: 24044017
THanks a ton alien109!!!

I appreciate the help...

0
 
LVL 1

Author Comment

by:StrongD1
ID: 24044229
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.  
0
 
LVL 12

Expert Comment

by:alien109
ID: 24044453
No worries. Glad to help. :)

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

Author Comment

by:StrongD1
ID: 24044478
Thanks again...  I really appreciate all of this.

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question