Help parsing an XML file ...

Hi all,
Hopefully someone can show me the errors of my ways here! I'm trying to parse an XML file and have hit a couple of road blocks.

This is the file I'm parsing:
Weather Information XML

I've attached my script below. I can read the file and was moving right along parsing it until I hit the first -date-, -epoch-, etc. part. I just can't seem to be able to read this part. I know I'm doing something wrong but this is all new to me. I commented out that part and was able to read in some more of the file and after the -/low- there are four items ... conditions, icon, skyicon and pop ... I can't figure out how to get them either.

Any help would be greatly appreciated!
 

Attachments

  • hac_weather.txt
    4.1 KB · Views: 12
I have not touched XML in a couple years now so I can't help much, but when I was working with it, I found oXygen to be a very useful tool.
It's not free, but it has a full functioning 30 day trial.

Hi all,
Hopefully someone can show me the errors of my ways here! I'm trying to parse an XML file and have hit a couple of road blocks.

This is the file I'm parsing:
Weather Information XML

I've attached my script below. I can read the file and was moving right along parsing it until I hit the first -date-, -epoch-, etc. part. I just can't seem to be able to read this part. I know I'm doing something wrong but this is all new to me. I commented out that part and was able to read in some more of the file and after the -/low- there are four items ... conditions, icon, skyicon and pop ... I can't figure out how to get them either.

Any help would be greatly appreciated!
 
... until I hit the first -date-, -epoch-, etc. part. I just can't seem to be able to read this part.

The following line in the supplied code: Set oNodeList = xmlfile.getElementsByTagName("date") will return a collection of all "date" nodes found in "xmlfile". Therein lies the problem ...

The first "date" node is a child of the "txt_forecast" node, it does not contain "epoch", and that's why oNode.selectSingleNode("epoch").Text fails. You can see that in the following Wunderground XML data (line 3). One solution is to use XPATH notation and indicate the precise path to the desired nodes: Set oNodeList = xmlfile.getElementsByTagName("forecast/simple_forecast/forecastday/date")

There's more than one way to process XML data. You've taken the approach of using getElementsByTagName repeatedly and that's not entirely necessary. XML data can be processed efficiently using iteration (i.e. looping through the nodes in a collection). For example, at the point your code uses Set oNodeList = xmlfile.getElementsByTagName("forecastday") it can proceed to process all child nodes of each "forecastday" node (period, date, high, low, conditions, etc) without using "getElementsByTagName" again. Let me know if you need an example of this approach.
 
123 to the rescue again! I would love to see an example.

EDIT: I now have a working script! I just could not get Set oNodeList = xmlfile.getElementsByTagName("forecast/simple_forecast/forecastday/date") to work until I changed it to Set oNodeList = xmlfile.getElementsByTagName("forecast/simpleforecast/forecastday/date")! :D


Thanks again for your time!
 
Thanks again for your time!
You're welcome! I'm laid up in bed with a nasty cold so my being bed-ridden (and bored) works to your advantage.

The attached archive contains getXML.vbs that, when executed, will display two dialog boxes, each containing data retrieved from Wunderground's XML feed. The example demonstrates some of the techniques I employed to extract data from Environment Canada's XML feed (a more complex XML schema). It also shows two different ways of acquiring "high" and "low" temperature data.

I think you'll have no trouble modifying the code so that it'll put the extracted data into a Homeseer-friendly INI file.

Here's a barebones version of getXML.vbs ... use it to get an overview of the process. I don't recommend running this snippet of code because it uses wscript.echo to display each extracted value (and that's a lot of popup messageboxes!):

Code:
set oXML = CreateObject("Microsoft.XMLDOM") 
oXML.validateOnParse = false 
oXML.async = false

sURL = "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=12345"

If oXML.Load(sURL) Then
	for each oNode in oXML.GetElementsByTagName("forecast/simpleforecast/forecastday") ' get appropriate forecastday nodes
		for each oChild in oNode.childNodes ' iterate through child nodes of each forecastday node
			with oChild
				select case .nodeName
					case "period": 	wscript.echo .text		
					case "date" ' iterate through the child nodes of the date node
						for each oItem in .childNodes
							select case oItem.nodeName
								case "epoch": wscript.echo oItem.text
								case "monthname": wscript.echo oItem.text
							end select
						next
					case "high" ' use selectSingleNode to get the desired node
						wscript.echo .SelectSingleNode("fahrenheit").text
				end select
			end with
		next
	next
else
	wscript.echo "Error. Unable to acquire XML weather data."
end if
 

Attachments

  • getXML.zip
    883 bytes · Views: 6
...I just could not get [it] to work until I changed it to Set oNodeList = xmlfile.getElementsByTagName("forecast/simpleforecast/forecastday/date")! :D
A little slip on my part ... there's no underscore in the Wunderground's "simpleforecast". One more thing (you may have already noted), capitalization is significant; "SimpleForecast", "simpleForecast", and "simpleforecast" are not interchangeable.

BTW, test your code by running it with different zip codes. When working with Environment Canada's XML feed, I discovered that sometimes not all nodes are available or the nodes may have no values. Your code should handle these conditions without failing. For example, "selectSingleNode" is very convenient to use but bombs if the node is not found.
 
Got another one for you.

In my quest to find the best way to get the data out of the xml file, I'm trying to parse directly from the asp page. I've had great sucess so far except for one peice of the puzzle. Let me show you what I'm doing so far.

This is a piece of code at the top of my page. Everything works great here.
Code:
	<script type="text/javascript">
	function parseXML()
	{
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async=false;
	xmlDoc.load("hac_weather.xml");

	try {document.getElementById("Period1_Icon").innerHTML = xmlDoc.getElementsByTagName("forecast/txt_forecast/forecastday/icon")[0].childNodes[0].nodeValue;}
	catch(err) {}

Once I'm within the body of the page, I'm doing stuff like this.
Code:
<b>Station ID: </b><span id='StationID'></span>
Which shows up on the page as Station ID: KB12345
Works as expected and that is great.

Now the problem I'm having is when I want to put that 'span id' into an 'img' tag. I can't seem to figure out the way to code this.
Code:
<img src='images/" & "<span id='Period1_Icon'></span>" & ".gif' align='left' width='40' height='40'>

What I need from this is
Code:
<img src='images/cloudy.gif' align='left' width='40' height='40'>
So the pricture will show on the page, but it doesn't work.

How can I get the value of the 'span' to just show up in the 'img' tag?

Thanks again for all your help!
 
Back
Top