Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Load XML File with JAvascript Clientside

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
StrongD1
Asked:
StrongD1
  • 8
  • 5
1 Solution
 
StrongD1Author 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>
0
 
alien109Commented:
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
 
alien109Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alien109Commented:
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
 
StrongD1Author 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\
0
 
StrongD1Author 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?

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

Thanks
0
 
StrongD1Author 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?
0
 
alien109Commented:
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
 
StrongD1Author Commented:
THanks a ton alien109!!!

I appreciate the help...

0
 
StrongD1Author 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.  
0
 
alien109Commented:
No worries. Glad to help. :)

Here's a great site to help you with all things web...
http://www.w3schools.com/
0
 
StrongD1Author Commented:
Thanks again...  I really appreciate all of this.

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now