View Full Version : Accessing the XML file through JavaScript instead of an HTML script
Erica_qt
03-26-2003, 01:06 AM
Hi, I have a script that me and a friend were trying to figure out, to access a .xml file in order to insert information into the HTML file. The following HTML script is what has been being used to access the XML file, however, there are problems when trying to do certain things using this method (In order to save anyone much confusion, I'll only describe exactly what the problems are if it's absolutely necessary, but I think the following information is good enough to describe what we need):
<span id="showPRICE" DATASRC="#albums" DATAFLD="ARTISTNAME"></span>
My friend said that it could be done if it could access the XML file through an equivalent JavaScript rather than HTML script.
There was another post on this forum that I think wants the same thing at:
http://www.codingforums.com/showthread.php?s=&threadid=16562
And on that page, it links to:
http://www.patrick-brennan.com/javascript/xml_sort_table.html
But the script on that page is very very confusing. How do I access an individual field in the XML file rather than a whole group? Like I said, we want it to be directly equivalent to this HTML script, but using JavaScript instead:
<span id="showPRICE" DATASRC="#albums" DATAFLD="ARTISTNAME"></span>
liorean
03-26-2003, 01:44 AM
Loading an xml file can be done through several different mechanisms, the one you'll be interested in is likely loading an xml document object into a reference variable.
var
src=[string XMLFile],
doc,
i;
function handleXml(doc){
/*\
| Handle xml document here.
| You can use DOM 1 Core and most of DOM 2 Core.
| Note that you will need to check whether you
| should use namespaced or non-namespaced functions
| if you want to edit the imported xml document.
\*/
}
switch((i=(document.implementation&&document.implementation.createDocument)||top.ActiveXObject||null)){
case ActiveXObject:
doc=new i('Microsoft.XMLDOM');
doc.async=false;
handleXml(doc);
break;
case document.implementation.createDocument:
doc=new i('','',null);
doc.load(src);
doc.onload=function(){handleXml(this)};
break;
default:
alert("User agent lacks xml importation mechanism");
}
(This code is just written and untested, so it might not work without bugfixing. Especially I wonder whether user agents that doesn't support document.implementation will let it pass or throw an error. Is nn4/ie4/op<7/minor browsers interesting for you?)
Erica_qt
03-31-2003, 03:17 AM
I couldn't understand any of that, I'm not too great at programming, so I didn't even know where to start with that...
However, I was browsing through search engines trying to find an answer, and I found EXACTLY what I needed... But I don't know how to make it work slightly different. They only have one group in their XML file... I need many many groups, and to be able to access them individually... Does anyone know what I mean?
This information is in "note.xml"
<?xml version="1.0" encoding="ISO8859-1" ?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
and this is in "note.html"
<html>
<head>
<script language="JavaScript"
for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
nodes = xmlDoc.documentElement.childNodes
to.innerText = nodes.item(0).text
from.innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text
</script>
<title>HTML using XML data</title>
</head>
<body bgcolor="yellow">
<h1>Refsnes Data Internal Note</h1>
<b>To: </b><span id="to"></span>
<br>
<b>From: </b><span id="from"></span>
<hr>
<b><span id="header"></span></b>
<hr>
<span id="body"></span>
</body>
</html>
Erica_qt
03-31-2003, 11:48 PM
Does anyone know how to do this? Is it too hard, is that why there is no response yet? Or is it just not possible?
liorean
04-01-2003, 12:04 AM
Oh, there's plenty of people here who know how to do it. The reason there's no response before now is not because it's too hard. It's not 'not possible'.
It's just that some things take time to code for. You're asking for something specific enough to require a final functionality but generic enough that we can't be sure what you want. And we're lazy. (And this isn't the most frequented forum, as you can see by the number of threads in it.) Give it a shot, explain the structure of the xml you want to dissect and what you want to read out from it. Explain what you want us to do with the information extracted from the file.
(Oh, just a sidenote, do you know anything about the DOM and how it works? That would make things easier for us.)
Erica_qt
04-01-2003, 03:31 AM
Sorry, I didn't mean to sound impatient. I would wait up to a week without comenting on the fact that there were no replies. Except that I've posted on this forum before (but lost my account and password, so I made a new one) a while back and usually got VERY quick responses, sometimes within minutes. And so that's why I thought it a little unusual that there were no replies yet.
Actually, my friend convinced me to describe the specific problem we were having, but since it was significantly different from this (it is related to the script in this post, but this post isn't necessarily related to it) that I just put it in a new post. But it hasn't gotten any replies. The URL for that post is:
http://www.codingforums.com/showthread.php?s=&threadid=17039
Anyway, the reason we need to be able to access the XML file with javascript is because he figured that he could get the script to work if he made variables equal whatever was in the specific XML fields, THEN insert it into the script.
liorean
04-01-2003, 03:27 PM
Erica if you tell me a few things, I could write a quite simple script doing what you need:
0. What input do you have for telling the script how to handle the xml (or in this case, determining what entry to handle)? Hard coded, cookies, user agent, prompt, links, forms, serverside determination etc.
1. What's the structure of each individual entry in the xml file? (I gather you wanted to get a single entry out of many. Am I right?)
2. When you have read the entry into the javascript, what processing (modification, calculation, etc.) do you want to do?
3. How do you want to output the result?
Try to be as concise as possible in explaining.
Charliers
02-23-2005, 02:53 PM
I understand what you want because I would like to do the same ..
Taking an exemple :
<player>
<name>toto</name>
<age>21</age>
<sex>man</sex>
...
</player>
We see in the exemple given before a javascript code that access to the i-th child of the <player> node..
But if you change the XML file your HTML page will be destroyed then!
What you want is to know how we can directly access the child "name" or "age" .. like that, even if someone add a child to the player node.. your HTML page will still correct!
.. I try to find some exemples, but for the moment, I have nothing!
Frederic.
codegoboom
02-24-2005, 11:12 PM
Uh, there are several ways to reference nodes... here are a couple:
xmlDoc.selectSingleNode("//name").text;
xmlDoc.getElementsByTagName("age").item(0).text;
Just between you & me, the MSXML (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmmscXML.asp) reference contains many examples. ;)
mattyod
02-25-2005, 10:03 AM
You might find this thread useful as I have just been going through a similar process :)
Same question in Javascript forums (http://www.codingforums.com/showthread.php?t=53151)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.