Reading XML Files in WSH - What to do when it doesn’t work
(Page 6 of 6 )
Scripting the XML Parser is pretty straightforward as far as controls go, but that doesn’t mean that you won’t run into any problems. So I decided to take a moment and talk about the most common problem you may run into.
As you’re trying these examples you may be wondering, “Why can’t I load my document?”
The most common pitfall associated with the XML Parser is a document not loading. In other words, your script doesn’t return any errors, but there doesn’t seem to be any information available. There’s no document tree.
If your document isn’t loading, you should first double check the path to your XML file. The XML Parser’s load method will not generate a VBScript error if it cannot find the document. If you’re positive that the path is correct, your problem may lie a little deeper.
Remember that we’re actually creating an XML Document object using the XML Parser. The Parser expects your XML file to contain valid XML code. The Parser actually validates the code before returning an XML Document object. This means that if your XML file will not validate, the parser will not return a reference to it.
While you can selectively disable this validation check, I’m not going to show you how. It’s just a bad idea all around to attempt using an XML file that doesn’t contain validated code. It can produce highly unexpected results.
Luckily for us, the XML Parser does provide us an error object that can shed some light on why the file won’t validate in the first place.
If Not objXmlDoc.load("C:\Playlist.xml") Then
Set objParseError = objXmlDoc.parseError
With objParseError
strErrText = "The document failed to load." & vbCrLf & VbCrLf & _
"Error #: " & .errorCode & ": " & .reason & _
"Line #: " & .Line & VbCrLf & _
"Line Position: " & .linepos & vbCrLf & _
"Position In File: " & .filepos & vbCrLf & _
"Source Text: " & .srcText & vbCrLf & _
"Document URL: " & .url
End With
MsgBox strErrText, vbExclamation
WScript.Quit
End If
This code sample demonstrates how you can wrap the Load method to determine whether a document was loaded successfully. If it wasn’t, it uses the XML Parser’s ParseError object to display a message box containing more detailed error information. This will display the same messages you would see if you tried to view your XML file in Internet Explorer.
I certainly don’t have the space to document every possible error here, but you should have enough information to find the problem on your own. Most typically, the problem will be a malformed XML file. You may be using illegal characters such as the ampersand instead of using their HTML entity.
As you can see, WSH does allow you to use XML as a viable database source. So why is this becoming so popular? First off, XML is a flat text file so it is very portable and cross-platform compliant. Second, it doesn’t require any proprietary database engines.
In any case, whatever your own reasons might be for using XML files, you should be well on your way. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |