Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML & ASP

Status
Not open for further replies.

cm80

Technical User
May 3, 2001
85
US
Hi,
I am new to XML and I was wondering if someone could give me a helping hand parsing an XML document with ASP.

I have the XML page loading into the ASP page fine, I just need a hand with referencing and formatting particular elements.


For example with the XML document below, what I want to write out is:

Wednesday 20th Feb
Day
Temperature:12
Wind:Light Breeze

Night
Temperature:22
Wind:Light Hurricane

The XML is:

-<DOCUMENT>
-<WEATHER type=&quot;Region&quot;>
<DATE>Wednesday 20th Feb</DATE>
- <FORECAST time=&quot;Day&quot;>
<TEMP>12</TEMP>
<WIND>Light Breeze</WIND>
</FORECAST>
- <FORECAST time=&quot;Night&quot;>
<TEMP>22</TEMP>
<WIND>Hurricane</WIND>
</FORECAST>
</WEATHER>
</DOCUMENT>


I'm not too worried about formatting for the moment so I want to avoid XSL if possible.
If anyone could help I would really appreciate it! Thanks!
 
Try this (assuming your file is called &quot;text.xml&quot;):

dim objXML
dim objNode
dim strDay
dim strDayTemp
dim strNightTemp
dim strDayWind
dim strNightWind

Set objXML = Server.CreateObject(&quot;MSXML2.DOMDocument.3.0&quot;)
objXML.async = False
objXML.load(Server.MapPath(&quot;test.xml&quot;))

If (objXML.parseError.errorCode <> 0) Then
Response.Write &quot;XML &quot; & strType & &quot; error - &quot; & &quot;<br>&quot;
Response.Write &quot;Error Reason: &quot; & objXML.parseError.reason & &quot;<br>&quot;
Response.Write &quot;Source: &quot; & objXML.parseError.srcText & &quot;<br>&quot;
Response.Write &quot;Error Line: &quot; & objXML.parseError.line & &quot;<br>&quot;
Response.Write &quot;Error Position: &quot; & objXML.parseError.linepos & &quot;<br>&quot;
Response.End
Else
Set objNode = objXML.documentElement.SelectSingleNode(&quot;//DATE&quot;)
If not objNode Is Nothing Then
strDay = objNode.text
Set objNode = Nothing
End If
Set objNode = objXML.documentElement.SelectSingleNode(&quot;//FORECAST[@time='Day']/TEMP&quot;)
If not objNode Is Nothing Then
strDayTemp = objNode.text
Set objNode = Nothing
End If
Set objNode = objXML.documentElement.SelectSingleNode(&quot;//FORECAST[@time='Night']/TEMP&quot;)
If not objNode Is Nothing Then
strNightTemp = objNode.text
Set objNode = Nothing
End If
Set objNode = objXML.documentElement.SelectSingleNode(&quot;//FORECAST[@time='Day']/WIND&quot;)
If not objNode Is Nothing Then
strNightWind = objNode.text
Set objNode = Nothing
End If
Set objNode = objXML.documentElement.SelectSingleNode(&quot;//FORECAST[@time='Night']/WIND&quot;)
If not objNode Is Nothing Then
strNightWind = objNode.text
Set objNode = Nothing
End If

Response.Write &quot;<b>&quot; & strDay & &quot;</b><br><br>Day<br>Temperature: &quot; & strDayTemp & &quot;<br>Forecast: &quot; & strDayWind & _
&quot;<br><br>Night<br>Temperature: &quot; & strNightTemp & &quot;<br>Forecast: &quot; & strNightWind
End If

Set objXML = Nothing
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top