XML Parse
'===========================================================
' NAME: XmlParseError.vbs
' AUTHOR: Neal Walters
' DATE : 3/19/2006
' http://VBScript-Training.com
'===========================================================
Dim xmlDoc, xmlFileName
'allow drag/drop of filename onto this VB/Script
If WScript.Arguments.Count > 0 Then
xmlFileName = WScript.Arguments(0)
Else
xmlFileName = "c:\Camtasia Studio\VBScript-Training2\books.xml"
end If
'DOM = Document Object Model
'set xmlDoc = CreateObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
WScript.Echo "Filename=" & xmlFileName
' Validate the document using the MSXML parser.
xmlDoc.load (xmlFileName)
If xmlDoc.parseError.errorCode Then
' Do something with the error.
Wscript.Echo "Parse Error: " & vbcrlf & _
" Reason = " & xmlDoc.parseError.reason & vbcrlf & _
" Line = " & xmlDoc.parseError.line & vbcrlf & _
" linePos = " & xmlDoc.parseError.linePos & vbcrlf & _
" srcText = " & xmlDoc.parseError.srcText & vbcrlf & _
" ErrorCode = " & xmlDoc.parseError.ErrorCode & vbcrlf
WScript.quit
'else
' WScript.Echo xmlDoc.xml
End If
WScript.Echo xmlDoc.xml
WScript.Echo "The end"
'===========================================================
' NAME: XmlParseError.vbs
' AUTHOR: Neal Walters , Amerisoft Inc
' DATE : 3/19/2006
' http://VBScript-Training.com
'===========================================================
Dim xmlDoc, strMyXml
'DOM = Document Object Model
'set xmlDoc = CreateObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
' Validate the document using the MSXML parser.
strMyXml = _
"" & _
" " & _
" MacBeth" & _
" William Shakespeare" & _
" 1750" & _
" " & _
" " & _
" A Tale of Two Cities" & _
" Charles Dicks" & _
" 1875" & _
" " & _
""
xmlDoc.loadXML (strMyXml)
If xmlDoc.parseError.errorCode Then
' Do something with the error.
Wscript.Echo "Parse Error: " & vbcrlf & _
" Reason = " & xmlDoc.parseError.reason & vbcrlf & _
" Line = " & xmlDoc.parseError.line & vbcrlf & _
" linePos = " & xmlDoc.parseError.linePos & vbcrlf & _
" srcText = " & xmlDoc.parseError.srcText & vbcrlf & _
" ErrorCode = " & xmlDoc.parseError.ErrorCode & vbcrlf
WScript.quit
'else
' WScript.Echo xmlDoc.xml
End If
WScript.Echo xmlDoc.xml
WScript.Echo "The end"
'===========================================================
' NAME: XmlParseError3.vbs
' AUTHOR: Neal Walters , Amerisoft Inc
' DATE : 3/19/2006
' http://VBScript-Training.com
'===========================================================
Dim xmlDoc, xmlFileName
'allow drag/drop of filename onto this VB/Script
If WScript.Arguments.Count > 0 Then
xmlFileName = WScript.Arguments(0)
Else
xmlFileName = "c:\Camtasia Studio\VBScript-Training2\books.xml"
end If
'DOM = Document Object Model
'set xmlDoc = CreateObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.4.0")
WScript.Echo "Filename=" & xmlFileName
' Validate the document using the MSXML parser.
xmlDoc.load (xmlFileName)
If xmlDoc.parseError.errorCode Then
' Do something with the error.
Wscript.Echo "Parse Error: " & vbcrlf & _
" Reason = " & xmlDoc.parseError.reason & vbcrlf & _
" Line = " & xmlDoc.parseError.line & vbcrlf & _
" linePos = " & xmlDoc.parseError.linePos & vbcrlf & _
" srcText = " & xmlDoc.parseError.srcText & vbcrlf & _
" ErrorCode = " & xmlDoc.parseError.ErrorCode & vbcrlf
WScript.quit
'else
' WScript.Echo xmlDoc.xml
End If
' Everything the same
' SelectNodes expects a valid "Xpath" parameter
set nodes = xmlDoc.selectNodes("//book[@type='computer']")
' set nodes = xmlDoc.selectNodes("//book[year='2001']")
' set nodes = xmlDoc.selectNodes("//bookyear'2001']") ' bad illegal Xpath expression
For Each node In nodes
WScript.Echo node.xml
Next
WScript.Echo "The end"
|