MobileVB XML Parse Class - FAQ
MobileVB XML
Contact Us

Q: On PocketPC, setting XmlElement to nothing crashes the program

A: To keep the program from crashing, do the following for each of the XmlElement objects you create and then the XMLEngine boject:

Set oRoot = Nothing
oXMLEngine.UpdateXmlEvent Nothing
Set oXMLEngine = Nothing

Q: How do I start Parsing using the InetHTTP Ingot:

A: Create a form with a Timer and InetHttp Ingot.  Name the Timer: tmrProcess and the InetHTTP: InetOffice, then cut and paste this code into the form.

Option Explicit
Private oXMLEngine As New XmlEngine

Private iCancelTimeout As Integer
Private dCancel As Date
Private iStatus As eRemoteStatus
Private Enum eRemoteStatus
    rsUnknown = 0
    rsConnecting = 1
    rsConnected = 2
    rsWaitingForResponse = 3
    rsReceivingData = 4
    rsDataRecieved = 5
    rsError = 6
    rsDone = 7
    rsProcessing = 8
End Enum

Private Sub Connect()
    ' Prepare the XMLEngine for Parsing the incoming data
    oXMLEngine.BuildTreeDuringParse = True

    ' Sends the information
    iStatus = rsConnecting
    dCancel = DateAdd("s", iCancelTimeout, Now)
    InetOffice.URL = sURL
    InetOffice.Document = sPost
    ' Starts the Timer to check to see if the information has been complete processed
    tmrProcess.Enabled = True
End Sub

Private Sub InetOffice_Error()
    ' Error
    On Error Resume Next
    Select Case InetOffice.SystemError
        Case 1: UpdateStatus "Error: Too Many Clients"
        Case 2: UpdateStatus "Error: Handle Invalid"
        Case 3: UpdateStatus "Error: Param Invalid"
        Case 4: UpdateStatus "Error: Version Invalid"
        Case 5: UpdateStatus "Error: URL Buffer Too Small"
        Case 6: UpdateStatus "Error: URL Invalid"
        Case 7: UpdateStatus "Error: Too Many Sockets"
        Case 8: UpdateStatus "Error: No Request Created"
        Case 9: UpdateStatus "Error: No Connected"
        Case 10: UpdateStatus "Error: Invalid Request"
        Case 11: UpdateStatus "Error: Need Time"
        Case 12: UpdateStatus "Error: Host Name Invalid"
        Case 13: UpdateStatus "Error: Invalid Port"
        Case 14: UpdateStatus "Error: Invalid Host Address"
        Case 15: UpdateStatus "Error: Nil Buffer"
        Case 16: UpdateStatus "Error: Connection Timeout"
        Case 17: UpdateStatus "Error: Reslove Timeout"
        Case 18: UpdateStatus "Error: Send Request Timeout"
        Case 19: UpdateStatus "Error: Read Timeout"
        Case 20: UpdateStatus "Error: Buffer Too Small"
        Case 21: UpdateStatus "Error: Scheme Not Supported"
        Case 22: UpdateStatus "Error: Invalid Resposne"
        Case 25: UpdateStatus "Error: Setting Too Large"
        Case 26: UpdateStatus "Error: Setting Size Invalid"
        Case 27: UpdateStatus "Error: Request Too Long"
        Case 28: UpdateStatus "Error: Setting Not Implemented"
        Case 29: UpdateStatus "Error: Config Not Found"
        Case 30: UpdateStatus "Error: Config Can't Delete"
        Case 31: UpdateStatus "Error: Config Too Many"
        Case 32: UpdateStatus "Error: Config Bad Name"
        Case 33: UpdateStatus "Error: Config not Alias"
        Case 34: UpdateStatus "Error: Config Can't Point to Alias"
        Case 35: UpdateStatus "Error: Config Empty"
        Case 37: UpdateStatus "Error: Config Alias Error"
        Case 38: UpdateStatus "Error: No Wireless Interface"
        Case 39: UpdateStatus "Error: Encryption not avialable"
        Case 40: UpdateStatus "Error: Need to Retry Encryption Sequence Number"
        Case 41: UpdateStatus "Error: Need to Retry Encryption Public Key"
        Case 42: UpdateStatus "Error: Response Too Short"
        Case 43: UpdateStatus "Error: Mobitex Illegal OK Host"
        Case 44: UpdateStatus "Error: Mobitex Illegal Bad Host"
        Case 45: UpdateStatus "Error: HTTP Bad Request"
        Case 46: UpdateStatus "Error: HTTP Unauthorized"
        Case 47: UpdateStatus "Error: HTTP Forbidden"
        Case 48: UpdateStatus "Error: HTTP Not Found"
        Case 49: UpdateStatus "Error: HTTP Method not allowed"
        Case 50: UpdateStatus "Error: HTTP Not Acceptable"
        Case 51: UpdateStatus "Error: HTTP Proxy Authorization Required"
        Case 52: UpdateStatus "Error: HTTP Request Timeout"
        Case 53: UpdateStatus "Error: HTTP Conflict"
        Case 54: UpdateStatus "Error: HTTP Gone"
        Case 55: UpdateStatus "Error: HTTP Length required"
        Case 56: UpdateStatus "Error: HTTP Precondition Failed"
        Case 57: UpdateStatus "Error: HTTP Request Too Large"
        Case 58: UpdateStatus "Error: HTTP Request URI Too Long"
        Case 59: UpdateStatus "Error: HTTP Unsupported Type - " & InetOffice.ContentType
        Case 60: UpdateStatus "Error: HTTP Server Error"
        Case 61: UpdateStatus "Error: CTP Server Error"
        Case 62: UpdateStatus "Error: Type Not cached"
        Case 63: UpdateStatus "Error: Cache Invalid"
        Case 64: UpdateStatus "Error: URL Dispatched"
        Case 65: UpdateStatus "Error: Database Not Found"
        Case 66: UpdateStatus "Error: CTP Malformed Request"
        Case 67: UpdateStatus "Error: CTP Unknown Command"
        Case 68: UpdateStatus "Error: CTP Truncated"
        Case 69: UpdateStatus "Error: CTP Unknown Error"
        Case 70: UpdateStatus "Error: CTP Proxy Error"
        Case 71: UpdateStatus "Error: CTP Socket Error"
        Case 72: UpdateStatus "Error: CTP Invalid URL"
        Case 73: UpdateStatus "Error: CTP Referring Page Out of Date"
        Case 74: UpdateStatus "Error: CTP Bad Request"
        Case 76: UpdateStatus "Error: CTP Mail Server Down"
        Case 77: UpdateStatus "Error: CTP Host not found"
        Case 78: UpdateStatus "Error: CTP Content - Invalid Tag"
        Case 79: UpdateStatus "Error: CTP Content - Internal"
        Case 80: UpdateStatus "Error: CTP Content- Data End"
        Case 81: UpdateStatus "Error: CTP Content - Resource Too Big"
        Case 82: UpdateStatus "Error: CTP Content - No No Frames"
        Case 83: UpdateStatus "Error: CTP Content - Unsupported Content"
        Case 84: UpdateStatus "Error: CTP Content - Unsupported Encoding"
        Case 85: UpdateStatus "Error: CTP Content - Bad Form"
        Case 86: UpdateStatus "Error: CTP Content - Bad Form Missing Action"
        Case 87: UpdateStatus "Error: CTP Content - Bad Form Missing Method"
        Case 88: UpdateStatus "Error: CTP Content - No source data"
        Case 89: UpdateStatus "Error: CTP Content - Bad Image"
        Case 90: UpdateStatus "Error: CTP Content - Image Too Large"
        Case 91: UpdateStatus "Error: Mobitex Error Handled"
        Case 92: UpdateStatus "Error: Proxy Down - Bad Host"
        Case 93: UpdateStatus "Error: Host Connection Lost"
        Case 94: UpdateStatus "Error: Link Not Found"
        Case Else:     UpdateStatus "Error: " & InetOffice.ResponseInfo & " (" & InetOffice.ResponseCode & ")"
    End Select
    iStatus = rsError
End Sub

Private Sub InetOffice_ReceivedData(ByVal totalSize As Long)
    ' Received information
    On Error Resume Next
    ' Update the timeout value
    dCancel = DateAdd("s", iCancelTimeout, Now)
    ' Update the status
    UpdateStatus totalSize & " Bytes Received"
End Sub

Private Sub InetOffice_StateChanged(ByVal newState As Long)
    Dim sTemp As String
    ' State Changed
    On Error Resume Next
    ' Update the timeout
    dCancel = DateAdd("s", iCancelTimeout, Now)
    ' Process the status
    Select Case newState
        Case afINetHTTPStatusAcquiringNetwork
            ' Looking for network
            UpdateStatus "Acquiring Network"
        Case afINetHTTPStatusClosed
            ' Connection has closed
            UpdateStatus "Closed"
            iStatus = rsError
        Case afINetHTTPStatusClosingConnection
            ' Closing the connection
            UpdateStatus "ClosingConnection"
        Case afINetHTTPStatusConnected
            ' Connected
            UpdateStatus "Connected"
            iStatus = rsConnected
        Case afINetHTTPStatusConnecting
            ' Currently connection
            UpdateStatus "Connecting"
            iStatus = rsConnecting
        Case afINetHTTPStatusNameResolved
            ' Name was resolved
            UpdateStatus "Name Resolved"
        Case afINetHTTPStatusNew
            ' Connection just openned
        Case afINetHTTPStatusReceivingResponse
            ' Receiving information
            UpdateStatus "Receiving Response"
            iStatus = rsReceivingData
        Case afINetHTTPStatusResolvingName
            ' Resolving the host name
            UpdateStatus "Resolving Name"
        Case afINetHTTPStatusResponseReceived
            'We received a valid response so lets get the data
            UpdateStatus "Parsing Data..."
            iStatus = rsReceivingData

            ' Verify the data is valid
            If InetOffice.ResponseCode = 404 Then
                ' Unable to find document
                UpdateStatus InetOffice.ResponseInfo
                iStatus = rsError

            ElseIf InetOffice.ResponseCode = 403 Then
                ' Unable to access document
                UpdateStatus InetOffice.ResponseInfo
                iStatus = rsError

                ' Setup the XML Engine
                ' Start gathering the information from the web site
                sTemp = InetOffice.GetChunk(100)
                While Len(sTemp) > 0
                    ' loop until there is not mroe data to aquire
                    oXMLEngine.AppendAndParse sTemp
                    sTemp = InetOffice.GetChunk(100)

                ' Mark as received
                UpdateStatus "Received Data"
                iStatus = rsDataRecieved
            End If
        Case afINetHTTPStatusSendingRequest
            ' Send request
            UpdateStatus "Sending Request"
        Case afINetHTTPStatusUnsupported
            ' Unsupported status
            UpdateStatus "Unsupported"
        Case afINetHTTPStatusWaitingForResponse
            ' Waiting for response from server
            UpdateStatus "Waiting for Response"
            iStatus = rsWaitingForResponse
        Case Else
            UpdateStatus "State Changed: " & newState
    End Select
End Sub

Private Sub UpdateStatus(ByVal Value As String)
    ' Display the status change
End Sub

Private Sub tmrProcess_Timer()
    ' Decide if we need to enble the close butt
    Dim dNow As Date
    Dim iNowTime As Long
    Dim iCancelTime As Long
    On Error Resume Next
    If iStatus = rsError Or iStatus = rsUnknown Then
        ' Error when connectin.  Enable the close button
        lblTimeout.Caption = ""
        dCancel = NullDate
        cmdClose.Visible = True
    ElseIf iStatus = rsDataRecieved Then
        ' We have received all the data, now let's update the class information
        lblTimeout.Caption = ""
        dCancel = NullDate
        iStatus = rsProcessing
    ElseIf iStatus = rsDone Then
        ' the data has been received.
        lblTimeout.Caption = ""
        dCancel = NullDate
        cmdClose.Visible = True
    ElseIf Now > dCancel And Not dCancel = NullDate Then
        ' We have reached a timeout.  Return an error
        lblTimeout.Caption = ""
        dCancel = NullDate
        iStatus = rsError
        UpdateStatus "Timeout:  We have been waiting for too long to recieve information from the office.  Either you connect is no longer active, or you have a bad connect"
        cmdClose.Visible = True
    ElseIf Not dCancel = NullDate Then
        ' Update the timeout status
        iNowTime = (Hour(Now) * 3600) + (Minute(Now) * 60) + Second(Now)
        iCancelTime = (Hour(dCancel) * 3600) + (Minute(dCancel) * 60) + Second(dCancel)
        lblTimeout.Caption = "Timeout In: " & iCancelTime - iNowTime
    End If
End Sub

Private Sub ProcessXML()
    ' Process the informatoin received
End Sub

Q: How do I use the IXmlEvent Class

A:  The IXmlClass was added to help speed up processing of large XMLDocuments.  It will allows you process your XML Elements as they are being decoded.

Option Explicit

' Xml Parsing information
Implements IXmlEvent

' -----------------------------------------------------------------------------------
' Xml Events
' -----------------------------------------------------------------------------------
Private Sub IXmlEvent_ProcessXML(ByVal ElementName As String, oXmlElement As XmlElement)
    ' Process the XML Element
    Select Case ElementName
        Case "Fastrax.PartList.Part"
            ' Process this Parts element.  This element will be found more than once
            ' during the decoding process
        Case "Fastrax.TaskBook.Task"
            ' Process this Task element.  This element will be found more than once
            ' during the decoding process
        Case "Fastrax.Company"
            ' Process the Company element
    End Select
End Sub

Private Sub ProcesXMLFile()
    Dim sFileName As String
    Dim oCSVFile As CFileTextReadable
    Dim sLine As String
    Dim oFileMgr As New CFileManager
    Dim oXMLEngine As New XmlEngine
    Dim iOffset As Long
    Dim iNumBytes As Long
    Dim iFileSize As Long
    Dim oXmlEvent As IXmlEvent
    ' Reads an Exml File
    On Error Resume Next
    sFileName = "Fastrax.xml"

    ' Gets the Text File
    Set oCSVFile = oFileMgr.OpenReadOnlyAsText(sFileName)
    If Err.Number = 0 Then Exit Sub

    ' Creates the XMLEvent Boject
    Set oXmlEvent = Me

    ' Start processing the task information
    oXMLEngine.AddEventElement "Fastrax.PartList.Part"
    oXMLEngine.AddEventElement "Fastrax.TaskBook.Task"
    oXMLEngine.AddEventElement "Fastrax.Company"
    oXMLEngine.UpdateXmlEvent oXmlEvent
    ' Start processing the information from the CSV
    iOffset = 0
    iFileSize = oCSVFile.Size
        ' Reads the next line
        If (iFileSize - iOffset) < 1000 Then
            iNumBytes = (iFileSize - iOffset)
            iNumBytes = 1000
        End If
        sLine = oCSVFile.Read(iNumBytes)

        ' Update status
        iOffset = iOffset + Len(sLine)

        ' Process the information
        oXMLEngine.AppendAndParse sLine
    Loop Until oCSVFile.Peek = -1
    Set oCSVFile = Nothing
    ' Clean up
    Set oXMLEngine = Nothing
End Sub