Solved

VB 6 and SAX (reading an XML file being used by the DOM)

Posted on 2004-07-30
7
891 Views
Last Modified: 2008-01-09
I would like to create a systray application which will show the current status of an application.  The idea is that I will determine the status of a batch process by reading its corresponding XML log file.  The log file looks like this:
<log>
      <RunGroup logDate="2004/07/30 12:13:43 PM">
            <TaskGroup logDate="2004/07/30 12:13:43 PM" name="Backup XPS Directory">
                  <method logDate="2004/07/30 12:13:43 PM" name="zip" status="success">
                  </method>
                  <method logDate="2004/07/30 12:13:44 PM" name="copy" status="success">
                  </method>
            </TaskGroup>
      </RunGroup>
      <RunGroup logDate="2004/07/30 12:13:53 PM">
            <TaskGroup logDate="2004/07/30 12:13:53 PM" name="Backup XPS Directory">
                  <method logDate="2004/07/30 12:13:53 PM" name="zip" status="success">
                  </method>
                  <method logDate="2004/07/30 12:13:54 PM" name="copy" status="failed">
                  </method>
            </TaskGroup>
      </RunGroup>
</log>
The application creating this log file is doing so via the MSXML DOM (version 4).

When I attempt to access the file also using the DOM while the other process is writing to it I get an error stating that I cannot access it since another process is using it.
I am assuming that this is because the DOM can be used to write to the file as well.  However, in my case I only want to read from the file.
So, I am assuming that I have to use SAX to do this, but just can't get a grasp on how to do this.  I have read numerous Microsoft articles but still am not sure.

So, can anyone provide code to extract from the above the last status?  From the above it would be "failed".  Also, am I correct in saying that SAX will be able to access the file while the DOM still has a hold of it?  If this is incorrect, is there a way to actually do this?
Thanks
0
Comment
Question by:dtomyn
  • 3
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11681088
It would be no different with SAX.  You are going to have to retry until it has quit writing the Xml document.
0
 
LVL 5

Author Comment

by:dtomyn
ID: 11681179
Is SAX not like a read only recorset though?  Can you think of any other alternative (than to keep retrying)?   Hmmm... I suppose if the file is locked I can assume that the other process is working though... I was hoping to be able to display the method though in the tooltip.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11681488
>>Is SAX not like a read only recorset though? <<
In a sense, yes.  Although strictly speaking it is more like a firehose (forward-only, read-only) resultset.  But to continue your analogy, you cannot open (not even) a read-only recordset if the database is opened exclusively.  I suspect what is happening is that while it is writing the file is locked.  However, this has to be only very briefly, so a retry after one second should suffice.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Author Comment

by:dtomyn
ID: 11681573
Would you be able to post some code to read the last status attribute given the XML above (using SAX... my current method is using the DOM)?  Just for completeness I would like to try this.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11681822
Sorry cannot help you there, I have never used it.  Hopefully someone else can step up to the plate.
0
 
LVL 3

Accepted Solution

by:
aParser earned 250 total points
ID: 11992949
Based in part on http://builder.com.com/5100-6373-1054452.html

[CSAXLog.cls]

Implements MSXML2.IVBSAXContentHandler
Implements MSXML2.IVBSAXErrorHandler
Private moSaxReader As MSXML2.SAXXMLReader 'SAX reader
Public logDate As Date

Private Sub Class_Initialize()
    'set up the SAX reader
    Set moSaxReader = New MSXML2.SAXXMLReader
    'pass a reference to this object to use as the content handler...
    Set moSaxReader.contentHandler = Me
    'and error handler objects
    Set moSaxReader.errorHandler = Me
End Sub
 
Private Sub Class_Terminate()
    Set moSaxReader = Nothing
End Sub

Public Sub LoadLog(strFile As String)
    moSaxReader.parseURL strFile
End Sub
...
Private Sub IVBSAXContentHandler_startElement(strNamespaceURI As String, strLocalName As String, strQName As String, ByVal oAttributes As MSXML2.IVBSAXAttributes)
    ' add whatever conditions necessary here; "method" is only for illustration here
    If strLocalName = "method" Then
        logDate = CDate(oAttributes.getValueFromName("", "logDate"))
    End If
End Sub
...


[whatever routine you like]
    Dim o As CSAXLog
   
    Set o = New CSAXLog
    o.LoadLog strFilePath ' strFilePath is the full path to your log file
    Debug.Print "Last log date: " & o.logDate
    Set o = Nothing
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro which automatically send Excel spreadsheet to Outlook problem 2 65
VBA Shell can't Find Word document 11 92
Help me. 3 54
MS Access 03, TransferText, decimal places 8 69
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question