Email conversation
From | Feng Li |
To | Me |
Subject | XML importing script |
Date | 17 June 2004 22:10 |
Hi there,
i have a question regarding your script post on this page:
http://www.howtocreate.co.uk/jslibs/htmlhigh/importxml.html
what's the passed-in parameter 'oFunct' for? i m really confused about
what i gonna do about that? You said 'When the xml file has loaded, the
named function will be run, and will be passed a reference to the document
object of the XML file.' what function is that refers to? how is it
gonna to be run if it's not defined? because i didn't see its definition
within your code.
i've been trying to load XML files and display the content onto a HTML
page using JavaScript in Netscape. the code i used before works well in
IE but is not working in NETSCAPE7. so i have to start looking for
another way to do it. that's why i came across your script over the
internet. i really hope i can get this done coz i've been working on
this issue for weeks. it's very frustrated when found out not working in
NETSCAPE :(
thanks for your attention and waiting for your answer and help.....
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 17 June 2004 22:57 |
Feng,
You must define the function yourself. In that function, you should use W3C
DOM scripting to process the XML of the document. For example:
function useTheData(theDocumentObject) {
//do something with the XML document
var oPut = document.getElementById('myParaGraph');
oPut.innerHTML = theDocumentObject.childNodes[0].firstChild.nodeValue;
}
When you want to load the XML data (for example from yourData.xml), you
would call the importXML function like this:
var canItWork = importXML( 'yourData.xml', 'useTheData' );
if( !canItWork ) { alert('Import failed'); }
When the XML file is loaded, the function useTheData will be run
As an unrelated note, and please accept this as constructive criticism, I
would like to point out that the internet has now progressed beyond the
browser wars of 1997 and there are more browsers in this world than just
Netscape and Internet Explorer. My script is specially written to work in
Opera, Safari, ICEbrowser and (Internet) Explorer Mac as well.
hope this helps
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 18 June 2004 01:21 |
Attachment | A sample RSS feed XML file |
Hi Mark,
thanks for your help.
as for the browsers, my project is to support only IE4+ / NS5+. so i don't
have to worry about the others. would you mind if i ask you something you
may think stupid? because i am not familiar with XML. when writing the
function u told me, i tried to parse the elements of the attached XML file
"test.xml",
function useTheData(xmlDocObj) {
var test = xmlDocObj.getElementsByTagName("\ / \ /channel").length;
// am i supposed to use other tag?
alert(test);
//this gives me a zero, which i think it should be one, according to the file.
var test2;
if (test[0].getElementsByTagName("\ / \ / title").length = 0){
test2 = test[0].getElementsByTagName("\ / \ / item");
//alert(test2[0].firstChild.nodeValue);
// supposed to give me "This is my news feed test"
.......
} else {
test2 = test[0].getElementsByTagName("\ / \ / title");
}
}
in the XML file, since the most outer tag name is "channel", then "title"
(if there is a title for the news list) or "item", i don't know where my
logic of parsing is wrong. can you please point it out?
thanks again.
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 18 June 2004 08:38 |
> as for the browsers, my project is to support only IE4+ / NS5+. so i
> don't have to worry about the others.
if this is a site/project open to the public, as an Opera user, I find that
fairly insulting, considering that my browser is highly capable of doing
DOM scripts. so I hope I never have to use this project :)
Also, it is important to note that virtually no Mac users will use
Netscape/Mozilla, so they would not be able to use it either.
I sincerely hope that this project will only be used internally in a
controlled environment, since otherwise, you are alianating a large number
of users by defying the very principles of the way that the internet was
created.
From the looks of this, you are writing a tool to analyse RSS newsfeeds, so
with any luck, the browser support should not be too much of an issue.
Anyway, that aside, your function should be
function useTheData(xmlDocObj) {
//you were looking for a tag called " / /channel"
var test = xmlDocObj.getElementsByTagName("channel");
alert(test.length);
var test2;
//you made 'test' a number - I made it an object
//note, use ==, not = (but why do you want to see if there is no title tag?)
if (test[0].getElementsByTagName("title").length){
test2 = test[0].getElementsByTagName("item");
//warning, some browsers see the blank area between tags as being a node
//also, the firstChild (assuming there is no whitespace) is the title tag, not
//the text within it - that is a child node of the title tag
alert(test2[0].firstChild.firstChild.nodeValue);
}else{
test2 = test[0].getElementsByTagName("title");
}
}
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 22 June 2004 03:18 |
Attachment | A sample RSS feed XML file |
Thanks for pointing out the err. however, i've tried the correct way so
many times in my various testing. it just doesn't seem parsing the XML
tags. and no error thrown at all. i have no idea what went wrong this time
:(
function useTheData(xmlDocObj) {
......
var test = xmlDocObj.getElementsByTagName('channel');
alert(test.length); //it's still zero which supposed to be '1'.
......
}
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 22 June 2004 08:44 |
Ok, I noticed that your XML is invalid. You have two <channel> tags, but
only one </channel> tag. The <channel> tag after the </docs> tag should not
be there.
That is the cause of the problem. Remove the second <channel> tag and it
will work.
If you try this:
var test = xmlDocObj.getElementsByTagName('*');
alert(test.length);
This also says 0, meaning that no tags were recognised. The reson that no
tags were recognised is that XML must be perfectly formatted, or the
browser is required by the XML specification to produce a parse error.
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 22 June 2004 19:43 |
Attachment | A sample RSS feed XML file |
thanks again. i fixed the XML file but it still throws a zero for the
length of that collection object. how can i make the code working? this
RSS thing is really driving me insane.....
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 22 June 2004 23:06 |
For me, it worked fine. If your XML file is valid now, then it should be
working (in IE, I found that it is essential that your webserver sends a
content-length header for the XML file).
Do you have it online so that I can see it (not) in action?
If it helps, I have written an RSS parsing tool ...
http://www.howtocreate.co.uk/tutorials/jsexamples/rss.html
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 23 June 2004 02:08 |
Attachment | A sample RSS feed XML file |
Sorry to bother you these many times and thank you SO MUCH for the patience
and help^_*
I've renamed the testing HTML file and it seems to parse the tags now. yet,
i still can't make it display the content out of any XML file. i have been
giving an error in which the <DIV> id is not recognized. well, it's almost
the end of today. i m sure it's got to be some tiny stupid mistake i made
:P
just uploaded the test file to the server. please use the following URL
and see the source. that's what i've been working on for almost 3 weeks.
http://hawaiian.state.government.site/xxx/xxxxxxxxx/xxxx/xxx.xxxx
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 23 June 2004 08:22 |
<aside>
alert('Your browser is too old to support this site.');
aaarrrgh! Opera 7 is NOT OLD! it is much newer than even the latest IE
releases.
if you must put a stupid error message that unnecessarily blocks good
browsers, at least put one that is accurate:
'sorry, but this site has not been programmed to support your browser'
</aside>
right. back to the problem
<div id='tt'></div>
<script >
tt.innerHTML = loadXmlDoc("http://rss.com.com/2547-12-0-5.xml";);
</script>
this is invalid script. only IE and Opera support this syntax. The correct
way to do this (which, incidentally, works in _all_ DOM browsers; IE5+,
NS6+/Mozilla, Opera 7, Safari, ICEbrowser) is:
document.getElementById('tt').innerHTML = theText;
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 23 June 2004 20:03 |
Attachment | A sample RSS feed XML file |
it's finally approaching the end of getting this done, with your great
help! you are my only hope and saver. i really don't know how to express
my appreciation towards you. Thanks, Mark!
urrrrrr......you can ignore that stupid msg about browser then :P i
didn't remember what i have put in the ELSE section.
right now i fixed calling statement. the tag 'tt' is recognized. but the
return string is not received in HTML page, although an alert indicating
it's totally fine before its returning from the JAVASCRIPT function.
<script>
function loadXmlDoc(xmlDoc)
{
var xdoc;
var file;
if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
{
...IE stuff...
}
else if( document.implementation && document.implementation.createDocument )
{
...mozilla/netscape stuff...
}
else
{
alert('blablabla :P ');
}
}
function parseFun(xDoc)
{
...some code to parse the XML and convert it into HTML...
return strHTML;
}
</script>
<div id="tt">aaa</div>
<script>
var test;
test = loadXmlDoc('taxtest.xml');
document.getElementById('tt').innerHTML = test;
</script>
best wishes,
Feng ^_^
From | Me |
To | Feng Li |
Subject | Re: XML importing script |
Date | 23 June 2004 22:16 |
Right. you are close.
I think you have misunderstood how to use the function.
loadXmlDoc does not return any value, so you cannot assign it to a variable.
At the moment, parseXML returns the value. However, since it was called by
the event handler of the XML document handler, it does not return it
anywhere useful either.
Replace:
<script>
var test;
test = loadXmlDoc('taxtest.xml');
document.getElementById('tt').innerHTML = test;
</script>
with:
<script>
loadXmlDoc('taxtest.xml');
</script>
and instead, put the script to write the results into the parseXML function.
replace:
return strHTML;
with:
document.getElementById('tt').innerHTML = strHTML;
[I should have corrected this in an earlier email]
From | Feng Li |
To | Me |
Subject | Re: XML importing script |
Date | 25 June 2004 00:16 |
Attachment | it works now :D |
Hi Mark,
the RSS news link is finally shown!!! wow, i've been very happy for this
result, which i couldn't have it done without your help! before my first
writing to you, i've posted a lot of messages online in different forums
for help. few people responded and no one was as nice and patient as you
to answer my questions. thus, i can't help saying it again: Mark, thanks a
trillion!
best wishes,
Feng ^_^
If you are interested, prompted by this series of emails, I wrote a
script to
interpret RSS feeds. It is intended only to be educational, not to replace proper RSS clients or server-side interpreters.