Windows Scripting
  Home arrow Windows Scripting arrow Page 6 - Reading XML Files in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Reading XML Files in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-12-01

    Table of Contents:
  • Reading XML Files in WSH
  • Getting started
  • More advanced queries
  • Further filtering results
  • Constructing advanced queries
  • What to do when it doesn’t work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek