Solved

Load XML File with JAvascript Clientside

Posted on 2009-04-01
13
377 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
  • 8
  • 5
13 Comments
 
LVL 1

Author Comment

by:StrongD1
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Accepted Solution

by:
alien109 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Closing Comment

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

Thanks
0
 
LVL 1

Author Comment

by:StrongD1
Comment Utility
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
Comment Utility
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
Comment Utility
THanks a ton alien109!!!

I appreciate the help...

0
 
LVL 1

Author Comment

by:StrongD1
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again...  I really appreciate all of this.

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now