Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Fetch & Read XML Data into HTML File?

From: "Richard In Va." <Reply-none@---.--->
To: NULL
Date: 9/10/2008 10:48:00 AM

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>&deg;&frasl;
      <script type="text/javascript">document.write(d0lo)</script>&deg;</td>
<td><script type="text/javascript">document.write(d0cond)</script></td>
<td><script type="text/javascript">document.write(d0prec)</script>&#37;</td>
</tr>

<tr>
<td>Day #1 Forecast :</td>
<td><script type="text/javascript">document.write(d1hi)</script>&deg;&frasl;
       <script 
type="text/javascript">document.write(d1lo)</script>&deg;</td>
<td><script type="text/javascript">document.write(d1cond)</script></td>
<td><script type="text/javascript">document.write(d1prec)</script>&#37;</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.
+++++++++++


"Martin Honnen" <mahotrash@y...> wrote in message 
news:ONnLSHpEJHA.3408@T......
> Richard In Va. wrote:
>
>> <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>
>
>> <!-- 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/ 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent