Hello Martin,
Sorry about the location I had for <title>...</title>, oversight on my part
there!
I tried your code suggestion and I'm sorry but I keep getting a script
error, var "loc" is undefined in the line...
var dnamEl = loc.selectSingleNode('dnam');
I tried to resolve the error on my own without much luck. But in an earlier
reply (your reply #3, post #6) you suggested I might try using DOM scripting
or XSLT as a method. Went to w3schools.com and read up on XML DOM
http://www.w3schools.com/dom/default.asp , took their tutorial example and
was able to get it to work for my weather.
This is what I've come up with (loading the xml file from my local drive)...
<html>
<head>
<title>23218 XML-DOM v2</title>
</head>
<body>
<script type="text/javascript">
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load("23218.xml");
document.write("xmlDoc is loaded, ready for use");
}
catch(e) {alert(e.message)}
</script><br /><br />
<script type="text/javascript">
var d0hi = (xmlDoc.getElementsByTagName("hi")[0].childNodes[0].nodeValue);
var d0lo = (xmlDoc.getElementsByTagName("low")[0].childNodes[0].nodeValue);
var d0cond = (xmlDoc.getElementsByTagName("t")[4].childNodes[0].nodeValue);
var d0prec =
(xmlDoc.getElementsByTagName("ppcp")[0].childNodes[0].nodeValue);
var d1hi = (xmlDoc.getElementsByTagName("hi")[1].childNodes[0].nodeValue);
var d1lo = (xmlDoc.getElementsByTagName("low")[1].childNodes[0].nodeValue);
var d1cond = (xmlDoc.getElementsByTagName("t")[8].childNodes[0].nodeValue);
var d1prec =
(xmlDoc.getElementsByTagName("ppcp")[2].childNodes[0].nodeValue);
</script>
<table border ="1">
<tr>
<td>Day #0 Forecast :</td>
<td><script type="text/javascript">document.write(d0hi)</script>°⁄
<script type="text/javascript">document.write(d0lo)</script>°</td>
<td><script type="text/javascript">document.write(d0cond)</script></td>
<td><script type="text/javascript">document.write(d0prec)</script>%</td>
</tr>
<tr>
<td>Day #1 Forecast :</td>
<td><script type="text/javascript">document.write(d1hi)</script>°⁄
<script
type="text/javascript">document.write(d1lo)</script>°</td>
<td><script type="text/javascript">document.write(d1cond)</script></td>
<td><script type="text/javascript">document.write(d1prec)</script>%</td>
</tr>
</table>
</body>
</html>
This will output to screen the high/low temps, condition and precip% for day
#0 and #1.
This seems to work fairly well but I can't figure how to get the weekday as
in "Monday". You mentioned about the weekday earlier, but I'm not able to
work that out for some reason.
Loading the XML file locally is an effort to keep things in HTML for now and
just getting things to work. (Maybe later I can find a way to have the html
file call an external script to download the xml file upon page
load/refresh?
Using this approach, I'm having to count each occurrence of <hi>, <low>, <t>
and <ppcp> but it's not all that bad.
Using "document.write" seems alittle awkward, couldn't figure out how to
apply <span>...</span> either.
Anyway, your opinion on this method (XML DOM) would be most appreciated...
and if you can help me fix the weekday, I'll add you to my Christmas
shopping list...
Ref: http://xoap.weather.com/weather/local/23218?cc=&dayf=7 for the xml
file.
Thanks again Martin and sorry for all the trouble!
Richard in VA.
+++++++++++
Post by Richard In Va.<html>
<title></title>
<head></head>
No, that is not proper HTML, the title element belongs inside of the head
element and it is good practice to put script elements there too so use
<html>
<head>
<title>...</title>
<script type="text/javascript">
function loadXml (url)
{
// all code of that function goes here
var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
doc.async = false;
if (doc.load(url))
{
doc.setProperty('SelectionLanguage', 'XPath');
var locEl = doc.selectSingleNode('/weather/loc');
var dnamEl = loc.selectSingleNode('dnam');
var dnam = dnamEl.text;
document.getElementById('dnam').innerText = dnam;
var tmEl = loc.selectSingleNode('tm');
var tm = tmEl.text;
document.getElementById('tm').innerText = tm;
}
}
window.onload = function ()
{
loadXml('http://xoap.weather.com/weather/local/23218?cc=&dayf=7');
};
</script>
</head>
<body>
<table>
<tr>
<td><span id="dnam"></span></td> <!-- location of observation -->
<td><span id="tm"></span></td> <!-- time of observation -->
</tr>
</table>
</body>
</html>
Post by Richard In Va.<!-- Display Day 0 -->
<table>
<tr>
<td><span id="dayName"></span></td> <!-- day of week -->
<td><span id="t"></span></td> <!-- condition e.g. mostly sunny -->
<td><span id="hi"></span></td> <!-- day #0 forecast high temp -->
</tr>
</table>
As I said, the id must be unique so use e.g.
<span id="dayName0"></span>
<span id="t0"></span>
and so on.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/