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/8/2008 3:27:00 PM


"Martin Honnen" <mahotrash@y...> wrote in message 
news:u62sOPdEJHA.3476@T......
> Richard In Va. wrote:
>
>> Okay, I think I can follow that, but how do I single out the individual 
>> days when...
>>
>> <dayf>
>>     <day d="0" t="Monday" dt="Sep 8">
>>         <hi>90</hi>
>>         <low>70</low>
>>         <part p="d">
>>             <t>Mostly Sunny</t>
>>             <ppcp>20</ppcp>
>>             <hmid>66</hmid>
>>
>> I'll want the "Monday", <hi>, <low>, <t>, <ppcp> and <hmid> for each day 
>> #0-6
>> If you can show me how to get the "Monday" and 1 element within day #0 
>> I'll figure the rest.
>
> You would access a node list of 'day' elements with
>   var dayElements = doc.selectNodes('/weather/dayf/day');
> then you can loop through that list
>   for (var i = 0, l = dayElements.length; i < l; i++)
>   {
>     var dayElement = dayElements[i];
>     var dayName = dayElement.getAttribute('t');  // e.g. 'Monday'
>     var hiEl = dayElement.selectSingleNode('hi');
>     var hi = hiEl.text;
>   }
>
> As for inserting data you get from the XML document into your HTML 
> document, you can do that by either preparing the HTML with elements to 
> take the data e.g. you would put a span
>   <span id="dnam"></span>
> into your HTML document where you want to display that data, then you 
> could access that as
>   var dnamSpan = document.getElementById('dnam');
>   dnamSpan.innerText = dnam;
> where dnma is the variable in my earlier post.
> For the data of the days of the week you would probably use a HTML table 
> and populate that.
> Or alternatively you could use DOM scripting to create all those HTML 
> dynamically.
>
> XSLT is also a nice tool to transform XML to HTML, not sure if you would 
> want to try that.
>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/

Once again...Thanks Martin,

So your suggesting I do this...

<html>
<title></title>
<head></head>
<body>

<script language="JavaScript">

   var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
   doc.async = false;
   if (doc.load('http://xoap.weather.com/weather/local/23218?cc=&dayf=7'))

   {
     doc.setProperty('SelectionLanguage', 'XPath');
     var locEl = doc.selectSingleNode('/wheather/loc');
     var dnamEl = loc.selectSingleNode('dnam');
     var dnam = dnamEl.text;
     var tmEl = loc.selectSingleNode('tm');
     var tm = tmEl.text;
    }
   else
   {
     //deal with doc.parseError here
   }
</script>

<table>
<tr>
<td><span id="dnam"></span></td>    <!--  location of weather 
bservation  -->
<td><span id="tm"></span></td>          <!-- time of obsrvation  -->
</tr>
</table>

<!--  misc html content here  -->

<script language="JavaScript">

     var dayElements = doc.selectNodes('/weather/dayf/day');
     for (var i = 0, l = dayElements.length; i < l; i++)
   {
     var dayElement = dayElements[i];
     var dayName = dayElement.getAttribute('t');  // e.g. 'Monday'
     var hiEl = dayElement.selectSingleNode('hi');
     var hi = hiEl.text;
   }
</script>

<!-- Display Day 0  -->
<table>
<tr>
<td><span id="t"></span></td>     <!-- condition e.g. mostly sunny  -->
<td><span id="hi"></span></td>    <!--  day 0 forecast high  -->
</tr>
</table>

<script language="JavaScript">

     var dayElements = doc.selectNodes('/weather/dayf/day');
     for (var i = 0, l = dayElements.length; i < l; i++)
   {
     var dayElement = dayElements[i];
     var dayName = dayElement.getAttribute('t');  // e.g. 'Monday'
     var hiEl = dayElement.selectSingleNode('hi');
     var hi = hiEl.text;
   }
</script>

<!-- Display Day 1  -->
<table>
<tr>
<td><span id="t"></span></td>    <!-- condition e.g. mostly sunny  -->
<td><span id="hi"></span></td>   <!--  day 1 forecast high  -->
</tr>
</table>

<!-- misc html content here  -->

</body>
</html>
<!--  save files as HTA  -->

Coffeecup reports an error on line #15... var dnamEl = 
loc.selectSingleNode('dnam');
as "loc" being undefined... then nothing happens.

As you can see, I'm probably completely lost, but I wonder if DOM scripting 
would make things more simple for me.
And with HTA, I loose the browser toolbar and tabed browsing that I would 
have with a HTML file.

Thanks Martin,

Richard in VA.
+++++++++++









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