Link to home
Start Free TrialLog in
Avatar of cunninw
cunninw

asked on

Parsing an XML file using VBScript when the child nodes all have the same name

I have the following XML document (see Below) that I would like to get into the following variables:  
Subject =
Location =
Description =
Startdate =
Enddate =
etc.....

I can't figure out how to do this when the nodes have the same names.  Can someone please guide me in the right direction?
<?xml version="1.0" encoding="utf-8" ?> 
- <GetCalendarResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <Result>true</Result> 
  <ResultCode>0</ResultCode> 
  <Message>Success</Message> 
- <ExCalendar>
- <key>
  <string>subject</string> 
  <string>location</string> 
  <string>description</string> 
  <string>startDate</string> 
  <string>endDate</string> 
  <string>reminder</string> 
  <string>guid</string> 
  <string>recurring</string> 
  <string>daily</string> 
  <string>weekly</string> 
  <string>monthly</string> 
  <string>yearly</string> 
  <string>monthyear</string> 
  <string>interval</string> 
  <string>occurrences</string> 
  <string>dayofmonth</string> 
  <string>daysofweek</string> 
  <string>nthday</string> 
  <string>setpos</string> 
  <string>until</string> 
  <string>categories</string> 
  </key>
- <val>
  <string>Bizou Lunch - Stein, Roy, and Greg</string> 
  <string>Bizou on the downtown mall</string> 
  <string>Looking forward to getting together for lunch on Friday. Let's plan on eating outside if it isn't too hot.</string> 
  <string>06/12/2009 16:00:00</string> 
  <string>06/12/2009 17:30:00</string> 
  <string>15</string> 
  <string>00000000D86BEDA7E1BF7E49874304C29E5917AAA4A03100</string> 
  <string>False</string> 
  <string>False</string> 
  <string>False</string> 
  <string>False</string> 
  <string>False</string> 
  <string>0</string> 
  <string>1</string>  
  <string>0</string> 
  <string>0</string> 
  <string>0</string> 
  <string>0</string>  
  <string>0</string> 
  <string>01/01/0001 00:00:00</string> 
  <string /> 
  </val>
  </ExCalendar>
  <Hash>-1800054489</Hash> 
  </GetCalendarResult>

Open in new window

Avatar of flob9
flob9
Flag of France image

Please, show us the vbscript code used to parse the xml.
Avatar of cunninw
cunninw

ASKER


<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Base64Decode.asp" -->
<%
Dim AdminUser,AdminPass,OwnerName,WSHost,ValidUser
   AdminUser = "xxxxxxxxxxxx"
   AdminPass = "xxxxxxxxx"
   OwnerName = "xxxxxxx"
   WSHost = "http://mail.xxxxxxx.com"
 
Function GetClendarList(WSHost,AdminUser,AdminPass,OwnerName)
 On Error resume Next
    Dim MSXmlHttp, MSXmlDom, MSXmlUrl
    Set MSXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
    MSXmlUrl = WSHost & "/Services/svcOutlookAddin.asmx/RequestCalendarListEx?AuthUserName=" & Server.URLEncode(AdminUser) & "&AuthPassword=" & Server.URLEncode(AdminPass) & "&OwnerName=" & Server.URLEncode(OwnerName) & ""
    MSXmlHttp.Open "GET" , MSXmlUrl , False ,"",""
    MSXmlHttp.Send
    If Err.Number = 0 Then
		'Response.Write(Err.Number)
		'Response.Write(MSXmlHttp.Status)
		'Response.Write(MSXmlHttp.ResponseText)
		If MSXmlHttp.Status = 200 then
            Set objXML = Server.CreateObject("Microsoft.XMLDOM")
            objXML.async=false
			objXML.loadXML(MSXmlHttp.ResponseText)
			Set XMLNode = objXML.selectNodes("//CalendarMetaInfo")
			For Each objXMLDOMNode In XMLNode
            'Set MSXmlDom = Nothing
				for i = 1 to objXMLDOMNode.childnodes.length
				if objXMLDOMNode.childnodes(i-1).nodename = "Guid" then
				Guid = objXMLDOMNode.childnodes(i-1).text
				elseif objXMLDOMNode.childnodes(i-1).nodename = "Hash" then
				Hash = objXMLDOMNode.childnodes(i-1).text
				End IF
					On Error resume Next
					Set MSXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
					MSXmlUrl = WSHost & "/Services/svcOutlookAddin.asmx/GetCalendarEx?AuthUserName=" & Server.URLEncode(AdminUser) & "&AuthPassword=" & Server.URLEncode(AdminPass) & "&OwnerName=" & Server.URLEncode(OwnerName) & "&guid=" & Server.URLEncode(GUID) & ""
					MSXmlHttp.Open "GET" , MSXmlUrl , False ,"",""
					MSXmlHttp.Send
					If Err.Number = 0 Then
						'Response.Write(Err.Number)
						'Response.Write(MSXmlHttp.Status)
						'Response.Write(MSXmlHttp.ResponseText)
						If MSXmlHttp.Status = 200 then
						objXML.loadXML(MSXmlHttp.ResponseText)
						Set XMLNode2 = objXML.selectNodes("//val")
							Subject = objXMLDOMNode.childnodes(0).text
							Location = objXMLDOMNode.childnodes(1).text
							'Response.Write(Subject)
							'Response.Write("<BR>")
							'Response.Write(Location)
							'Response.Write("<BR><BR>")
						End IF
					End IF			
				Next
			Next
        End if
    End if
    'Set MSXmlHttp = Nothing
End function
ValidUser =   GetClendarList(WSHost,AdminUser,AdminPass,OwnerName)
%>

Open in new window

Avatar of cunninw

ASKER

This is the portion of code I am currently using to parse the XML in question:

objXML.loadXML(MSXmlHttp.ResponseText)
                                    Set XMLNode2 = objXML.selectNodes("//val")
                                          Subject = objXMLDOMNode.childnodes(0).text
                                          Location = objXMLDOMNode.childnodes(1).text
You have to store all the keys in an array, then parse the values and retrieve the key by index in the previously parsed array to know wich field it is.

This is a quite an horrible xml file ...

Tell us on wich step you have problems.
Avatar of cunninw

ASKER

Can you please give me some idea of how to get started in doing this?
Try this :
Dim objXML, Root, Keys, i
Set objXML = WScript.CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.load("test10.xml")
 
Set Keys = objXML.selectNodes("//GetCalendarResult//ExCalendar//key//*")
Set Values = objXML.selectNodes("//GetCalendarResult//ExCalendar//val//*")
 
For Each Key In Keys
	WScript.echo Key.text & " => " & Values(i).text
	i = i + 1
next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of flob9
flob9
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial