Email conversation
From | Neil K |
To | Me |
Subject | Need feedback using your script to import an RSS/XML file |
Date | 15 March 2008 18:06 |
Dear Tarquin:
I obtained most of this script from your website. I coudln't get it to
work even when I made some changes. I would just like it to execute and
display the output. There are two errors in Mozilla including "ImportXML
not defined", which I can't seem to resolve.
http://www.howtocreate.co.uk/jslibs/htmlhigh/importxml.html
I posted a script I wrote from scratch on this message board.
[URL]
the moderator "[name]" then directed me to your site and hence your
version of importing XML files..
Here is your modified script if you can figure out what I'm doing wrong.
I've spent many hours on it, so your help is highly appreciated.
Thank you very much for your time.
Neil
_____________________________________________________________________
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft Wordpad>
<meta name="Neil K Project" content="Miscrosoft Wordpad">
<title>Neil K project</title>
<script type="text/javascript">
var MWJ_ldD = [];
var myXML.xml;
var canItWork = importXML();
</script>
<script type="text/javascript">
function importXML( 'myXML.xml', 'runThis', false, 2000 ) {
[the contents of my XML importing functions]
var MWJ_ldD = [];
function importXML( oURL, oFunct, oNoRand, oDelay ) {
[the contents of my XML importing functions, again]
</script>
<b><font size=6 face=bold>View Articles</font></b>
<div id='myXML'>Enter XML file</div><BR>
<input id=T1 type=text style="width: 400"><P>
<input type=button value="Submit Query"
onclick="myXML.innerHTML = T1.value";>
<input type=button value="CONTINUE"
ONCLICK="importXML()";>
<script type="text/javascript">
function writeRSS()
{
objNodeList = CanItWork.getElementsByTagName("title");
var str = "The node list has " + objNodeList.length + " items.\n";
hWin=window.open("","Assignment","height=300,width=450");
for (var i=0; i < objNodeList.length; i++) {
str = str + (i + 1) + ": " + objNodeList.item(i).xml + "\n";
document.write('objNodeList');
}
}
</script>
</head>
<body onLoad="writeRSS">
</body>
</html>
From | Me |
To | Neil K |
Subject | Re: Need feedback using your script to import an RSS/XML file |
Date | 15 March 2008 21:41 |
Neil,
> There are two errors in Mozilla including "ImportXML not defined"
My contact page asks you to say what error messages _Opera_ says, not
Mozilla. Opera's messages are much more useful for me when debugging, since
they actually give a stack trace, and usually much more meaningful error
messages.
Aside from that, your error message is wrong, since you changed the case of
the word. ImportXML != importXML. This makes your message very misleading,
and made it look like a completely different error message.
> var myXML.xml;
That is not valid. The var keyword can only declare normal variables, not
properties. This entire <script> block is being ignored because of this
error.
> var canItWork = importXML();
You cannot call a function from a <script> block that runs before the
<script> block where you actually define the function.
> <script type="text/javascript">
You have removed my comments from the source code, which violates my license
terms. Please put them back, and follow the license terms here:
http://www.howtocreate.co.uk/jslibs/termsOfUse.html
> function importXML( 'myXML.xml', 'runThis', false, 2000 ) {
This is not a valid function declaration. Function declarations must only
have valid variable names in their parameter list. They cannot have string
references, boolean values, or numbers.
It would be much simpler for you to just use my script as an external script
file without modifying it in a way that doesn't work. The file contains
instructions on how to do this. Please follow those.
> function importXML( oURL, oFunct, oNoRand, oDelay ) {
I do not know why you duplicated it in the source code, but the second
version is the right one (the first you have changed so it is wrong).
> ONCLICK="importXML()";>
onclick="importXML(document.getElementById('T1').value,
'writeRSS',false,2000);">
> function writeRSS()
function writeRSS(CanItWork)
In fact, there are several mistakes in this function. Just replace it with
this:
function writeRSS(CanItWork) {
var objNodeList = CanItWork.getElementsByTagName("title");
var str = "The node list has " + objNodeList.length + " items.\n";
var hWin=window.open("","Assignment","height=300,width=450");
for (var i=0; i < objNodeList.length; i++) {
str = str + (i + 1) + ": " + objNodeList[i].firstChild.nodeValue + "\n";
hWin.document.write(str);
}
}
> <body onLoad="writeRSS">
This part is nonsense, since you already wrote the body contents (the form
inputs). The onload listener is also not needed.
Due to the number of mistakes you have made with this script, I think you
may benefit from working through tutorials on JavaScript and HTML. I offer
some on my site that should be of some help to you:
http://www.howtocreate.co.uk/tutorials/javascript/important
http://www.howtocreate.co.uk/tutorials/html/basics
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
From | Neil K |
To | Me |
Subject | Re: Need feedback using your script to import an RSS/XML file |
Date | 16 March 2008 02:06 |
Dear Tarquin:
Thank you very much for being so gracious and getting back to me so soon. I
am in process of reviewing your feedback. And yes I will make sure that
your remarks are included in any version of the script per your license
requirements.
I know it may appear as if I'm off the mark, but if you look at a previous
version of my script (included below) for importing XML files, I think
you'll understand where I was going. It was only at the suggestion of
"[name]" the moderator, that I switched to your version instead.
Thanks again,
Neil
[A script based on the now obsoleted methods from an early DOM 3 LS draft]
From | Neil K |
To | Me |
Subject | Re: Need feedback using your script to import an RSS/XML file |
Date | 18 March 2008 18:14 |
Dear Tarquin:
I went over the tutorial per your suggestion and I've made some changes.
But I'm still getting 3-4 errors in Opera. Any suggestions? What I'm
trying to do is to get the program to present the parsed output of the xml
file as a list on a second page (with author, description and link for
example) that one can then select which item they want to read. I hope I'm
not asking too much. I wil reenter any missing comments when I'm done per
the terms of your license and properly reference you as the source.
Thanks,
Neil
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft Wordpad>
<meta name="Neil K Project" content="Microsoft Wordpad">
<title>Neil K project</title>
<b><font size=6 face=bold>View Articles</font></b>
<div id='myXML'>Enter XML file</div><BR>
<input id=T1 type=text style="width: 400"><P>
<input type=button value="Submit Query"
onclick="myXML.innerHTML = T1.value";>
<input type=button value="CONTINUE"
onclick="importXML(window.T1.value)";>
<script type="text/javascript"><!--
//for older browsers like Netscape 4 ... if you care
window.onerror = function () { return true; }
//--></script>
<script type="text/javascript">
var MWJ_ldD = [];
var node_list[];
oURL=window.T1.value;
</script>
<script type="text/javascript">
function importXML( oURL, oFunct, oNoRand, oDelay ) {
[the contents of my XML importing functions]
oURL, oFunct, oNoRand, oDelay {
[the contents of my XML importing functions, again]
</script>
<script type="text/javascript">
/*************************************
DECLARE GLOBAL VARIABLES
*************************************/
var c
var d='';
var e='';
var f='';
var g='';
var xml_doc
/*************************************
MOZILLA - EXTRACT NODE INFORMATION
*************************************/
function readFileMoz()
{
node_list = MWJ_ldD.getElementsByTagName("title")
for (i = 0; i <= node_list.length-1; i++)
{
c = node_list[i].childNodes[1].childNodes[0].nodeValue
d = node_list[i].childNodes[3].childNodes[0].nodeValue
e = node_list[i].childNodes[3].childNodes[0].nodeValue
f = node_list[i].childNodes[3].childNodes[0].nodeValue
g = node_list[i].childNodes[3].childNodes[0].nodeValue
alert("C = " + c + " D = " + d + " E = " + e + " F = " + f + " G = " + g)
}
}
</script>
<script type="text/javascript">
hWin=window.open("","Assignment","height=300,width=450");
setTimeout(readFileMoz,1000);
readFileMoz();
</script>
</head>
<body>
</body>
</html>
From | Me |
To | Neil K |
Subject | Re: Need feedback using your script to import an RSS/XML file |
Date | 18 March 2008 20:28 |
Attachment | demo |
Neil,
> I've made some changes.
But not all the changes I told you to make in the last email. You need to
follow the rest of them.
For a start, you still have my code repeated twice in the source, one of
which is broken. It is also split into more than one script tag for no
reason, along with some extra stuff that is invalid ("var foo[]" instead of
"var foo = []"). Please follow the instructions from my last email where I
told you to reference my script file externally.
> <title>Neil K project</title>
The </head> and <body> tags belong here, or at least somewhere before the
form elements.
> onclick="importXML(window.T1.value)";>
This is still nothing like what I told you to use, and is using a completely
invalid element reference. Use what I told you to to use.
I don't know why you have chosen to make such a completely different
version, broken in whole new ways. It makes it impossible to offer
suggestions as to what to change, because you ignored my suggestions.
I have attached an example that will work, as long as you save a copy of my
XML importing script into the same folder. Do that, try it, see how it
works, and then build on it. Do not start again with a completely different
approach.
> What I'm trying to do is to get the program to present
> the parsed output of the xml file as a list on a second page
You are trying to open a popup without user interaction. It will be blocked
by popup blockers. The code you sent last time, combined with my corrections
from my last email, is the right way to do it (though it still needs the
popup to be opened directly by the click in order not to trigger popup
blockers).
Please, I know you looked at the tutorials, but due to the number of
mistakes you are still making (fundamental things like declaring variables
or functions), you obviously still need to actually learn what they are
trying to teach you.
You cannot do this just by brushing over a tutorial or hoping to find
specific pointers, you have to learn it all properly. It takes time to
learn, but scripts are not very forgiving, and you need to invest that time
if you want to be able to write scripts that work.
From | Neil K |
To | Me |
Subject | Re: Update - using your script to import an RSS/XML file |
Date | 19 March 2008 06:34 |
Tarquin:
Thank you for helping me with the revisions. I will go over the tutorial
several more times per your suggestion.
I am in process of adding one other feature to your version to make the list
items link selectable. It's not quite there, where each item is linked to a
URL. I'm assuming the 'link' node,
is in fact the firstChild of the firstChild (ie. firstChild of the 'title'
node which would depend on the individual DOM tree structure of each xml
file) I'll have to test it until I find the right node I guess.
There must be an easier way, that relies on the nameTags and not on
assumptions about whether the link node is a sibling or firstChild of the
'title' node, etc. Any suggestions. Thanks a million for the progress so
far.
Here is the altered code and beneath that is the "page source".
-------------------------------------------------------------
var str = 'The node list has ' + objNodeList.length + ' items.\n';
for( var i = 0; i < objNodeList.length; i++ ) {
str = str + (i + 1) + ': ' + "<option
value=objNodeList[i].firstChild.firstChild.nodeValue>" +
objNodeList[i].firstChild.nodeValue + "<\/option>" + '\n';
--------------------------------------------------------------
<pre>The node list has 21 items. 1: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>NYT > Books</option>
2: <option value=objNodeList[i].firstChild.firstChild.nodeValue>NYT >
Books</option> 3: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Books of The Times: The
Case for Another Drug War, Against Pharmaceutical Marketers' Dirty
Tactics</option> 4: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Magda Cordell McHale,
86, Futurist Thinker, Is Dead</option> 5: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>For Youngsters, Leaps
and Boundaries</option> 6: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Exhibition Review: A
Giant's Roaring, Faintly Echoed</option>
7: <option value=objNodeList[i].firstChild.firstChild.nodeValue>Western
Authors Celebrate a Master</option> 8: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>A $100 Million Donation
to the N.Y. Public Library</option> 9: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Neighborhood
Watch</option> 10: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>What's Left</option>
11: <option value=objNodeList[i].firstChild.firstChild.nodeValue>Woe Be
Gone</option> 12: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Wild and Crazy
Guys</option>
13: <option value=objNodeList[i].firstChild.firstChild.nodeValue>Obsessed
(Agog, Beset, Consumed, Driven, etc.)</option> 14: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>The Wrong War</option>
15: <option value=objNodeList[i].firstChild.firstChild.nodeValue>Hot or
Not</option> 16: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>I Feel Good</option>
17: <option value=objNodeList[i].firstChild.firstChild.nodeValue>The
Interrogator</option> 18: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Children's Books: Keep
On Truckin'</option>
19: <option value=objNodeList[i].firstChild.firstChild.nodeValue>Archive:
Book Review Podcast</option> 20: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>Essay: Admission
Impossible</option> 21: <option
value=objNodeList[i].firstChild.firstChild.nodeValue>The Funny Pages |
Sunday Serial: The Lemur</option> </pre>
From | Me |
To | Neil K |
Subject | Re: Update - using your script to import an RSS/XML file |
Date | 21 March 2008 10:30 |
Attachment | demo |
Neil,
> There must be an easier way, that relies on the nameTags and not on
> assumptions about whether the link node is a sibling or firstChild of
> the 'title' node, etc. Any suggestions.
Make objNodeList be a collection of all item elements (in RSS/Atom), then
for each item element, you get the title element like this:
theTitle = oneOfTheItemElements.getElementsByTagName('title')[0];
This is covered in the DOM Basics chapter of the JavaScript tutorial.
Then to get the text content within it, you need to get all of the child
nodes, which should all be text nodes, comment nodes, or CDATA nodes. the
CDATA and text nodes are the only ones you want. Start with an empty string.
Use a 'for' loop to loop through the childNodes collection of the title
element, and if the nodeType of the childNode is 3 (text node) or 4 (CDATA
node), then append its nodeValue to your string. You will now have the text
of the title.
> "<option value=objNodeList[i].firstChild.firstChild.nodeValue>" +
You need to end the string and append the variable:
"foo"+bar+"baz"
At the moment, you are including this literal string several times in your
output:
objNodeList[i].firstChild.firstChild.nodeValue
This is covered in the Operators and Variables chapters of the JavaScript
tutorial.
You also are using a very strange bit of markup there. Options are
completely inappropriate. You should be using relevant elements like headers
and paragraphs. The HTML tutorial gives more details about all of
these.
From | Neil K |
To | Me |
Subject | Re: Update - using your script to import an RSS/XML file |
Date | 3 April 2008 02:29 |
Attachment | Two versions of my last attachment, with minor changes |
Dear Tarquin:
I have revised the code many times, using your suggestions to no avail.
These are the two latest versions. I realize your time is very valuable and
appreciate any input at all.
Any suggestions? When I pick a selection, it won't go to the html page.
Opera gives the following error. It is the only one error.
4/objNodeList2[i].firstChild.nodeValue
Could not open file
And yet I have tested and this file which is a URL bears out. For instance
if I switch 'title' to 'link', in the following:
var objNodeList = CanItWork.getElementsByTagName('title');
it works perfectly well and even finds the right URL when selected. I
wonder why it wouldn't do the same as an OPTION=VALUE, SRC or HREF to the
TITLE?
Any Thoughts?
By the way, I researched the use of and tried implementing "foo"+bar+"baz"
but it only seemed to make things worse.
You also recommended that I don't use the Option Tag. Not sure why? It is
used extensively in DOM and HTML.
Thank you very much,
Neil
From | Neil K |
To | Me |
Subject | Re: Update - using your script to import an RSS/XML file |
Date | 3 April 2008 19:27 |
Attachment | Version of my last attachment, with an extra function generating some horrible HTML |
Dear Tarquin:
I made a separate revision to your original version to implement a
getChildren function so I could list children after each title. I get the
following error and I believe it has something to do with timing
(settimeout).
Here's the error using Opera and attached is the code: Any suggestions?
Thanks.
---------------------------------------------------------------------------
JavaScript - file://localhost/C:/[university]/homework4/importpop10.html
Unknown thread
Error:
name: ReferenceError
message: Statement on line 31: Reference to undefined variable: objNodeList
Backtrace:
Line 31 of inline#1 script in
file://localhost/C:/[uni]/homework4/importpop10.html
var x = objNodeList.childNodes;
Line 16 of inline#1 script in
file://localhost/C:/[uni]/homework4/importpop10.html
x = getChildren(objNodeList);
Line 1 of script
if (MWJ_ldD[0].readyState == 4 && MWJ_ldD[0].status < 300)
At unknown location
[statement source code not available]
From | Me |
To | Neil K |
Subject | Re: Update - using your script to import an RSS/XML file |
Date | 3 April 2008 23:57 |
Neil,
> message: Statement on line 31: Reference to undefined variable:
> objNodeList
You should be able to see from this line what the problem is. You are
trying to use a variable called objNodeList inside a function, but it
doesn't exist inside that function because you never passed it to that
function and it is not in the global scope. My tutorial deals with this
in the chapter on functions.
> Opera gives the following error. It is the only one error.
> 4/objNodeList2[i].firstChild.nodeValue
> Could not open file
This does not look like any kind of error that Opera would produce. It
does not make any sense, especially since your examples never use that
construct as a piece of code. Was that supposed to be a screenshot that you
were pasting into an email or something?
If it is complaining about the first line (no idea why you have "4/" in
front of it), then that is probably because you are stepping through a
collection called objNodeList with the for loop, then expecting to retrieve
values from a collection called objNodeList2 when the lengths of the two
collections are probably different, so at some point you are stepping off
the end of the objNodeList2 collection, causing the error.
> By the way, I researched the use of and tried implementing
> "foo"+bar+"baz" but it only seemed to make things worse.
Then it would seem that you misunderstood where I was telling you to
apply it. It should be applied to this:
"<option value=objNodeList[i].firstChild.nodeValue>"
That is a literal string, not a concatenation of strings and variable
values.
"<some thing=somethingelse>"
is not the same as
"<some thing="+somethingelse+">"
I can see from all of your local URLs that this is a homework
assignment. Try asking your teacher what you are doing wrong here - I am
sure they will be happy to explain.
> You also recommended that I don't use the Option Tag. Not sure why?
> It is used extensively in DOM and HTML.
Nobody is trying to say it doesn't exist or work. Just you are using it
for completely the wrong reason, in the wrong place, where it has
absolutely no effect because it doesn't belong there. It belongs inside
a <select> input, which did not exist in your original example at all.
Your <option> elements were being ignored completely by the browser.
In your revisions, you have a <select> input though you are still using
it for completely the wrong purpose (it's a form input not an output).
You are also putting the options inside a <pre> inside the <select>,
which is also completely invalid and will not work. You really need to
learn what all the various HTML tags are, what they do, and where they
belong in a document. My HTML tutorial covers this, and your teacher should
also be able to help you.
Note that I am probably not going to be available to answer emails for a
number of months. Please ask your teacher for any further help that you
need.