Results 1 to 6 of 6

Thread: [VB6] SAX: Not just for XML

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Lightbulb [VB6] SAX: Not just for XML

    MXHTMLWriter is a handy feature added to MSXML SAX2 in version 6.0, but few have probably heard of SAX and few still of MXHTMLWriter.

    See MXHTMLWriter CoClass for an overview.

    There are several ways to use MXHTMLWriter but here I'll turn it "inside out" by explicitly raising events to it via IVBSAXContentHandler instead of letting other parts of MSXML raise the events. This is a very basic example showing how to do that to write HTML, and in this case the demo involves simple reporting.

    Depending on your purpose you might want the results in different ways. Here I show how to get file output, String output, and Byte array output (since for that we can get UTF-8 or other character encodings).

    It should be plenty speedy enough for most purposes:

    Name:  sshot.png
Views: 1255
Size:  3.1 KB

    Here is the crux of the demo:

    Code:
    Private Sub Report(ByRef Dest As Variant, Optional ByVal Encoding As String = "ASCII")
        'Dest:     Can be an instance of an IStream implementation or a String.
        '
        'Encoding: Can be "UTF-8" or "Windows-1252" or "UTF-16" etc. as desired.
        '          Always ignored for String output which is always UTF-16
        '          ("Unicode").
        Const REPORT_TITLE As String = "January 2009 Sales"
        Const CSS_STYLES As String = vbNewLine _
            & "*{font:normal normal normal 8pt Arial;}" & vbNewLine _
            & "th,td{border:1px solid black;}" & vbNewLine _
            & "th{background-color:royalblue;color:white;font-weight:bold;}" & vbNewLine _
            & "td{background-color:white;color:green;}" & vbNewLine _
            & "table,th,td{border-collapse:collapse;}" & vbNewLine _
            & ".SH{color:red;}"
        Dim Attrs As MSXML2.SAXAttributes60
        Dim Handler As MSXML2.IVBSAXContentHandler
        Dim Writer As MSXML2.MXHTMLWriter60
        Dim FieldsUB As Long
        Dim Fields() As ADODB.Field
        Dim Col As Long
        Dim Row As Long
        Dim LatitudeField As Long
        Dim Value As Variant
    
        Set Attrs = New MSXML2.SAXAttributes60
        Set Writer = New MSXML2.MXHTMLWriter60
        Set Handler = Writer
        With Writer
            .disableOutputEscaping = False
            .indent = True
            .Encoding = "ASCII"
            .byteOrderMark = True 'Has no effect for 8-bit encodings or any String output.
            .output = Dest 'Can be an IStream implementation, or a String value to set
                           'the output type to String.
        End With
        With RS
            .MoveFirst
            FieldsUB = .Fields.Count - 1
            ReDim Fields(FieldsUB)
            For Col = 0 To FieldsUB
                Set Fields(Col) = .Fields(Col)
                If Fields(Col).Name = "Latitude" Then LatitudeField = Col
            Next
        End With
        With Handler
            .startDocument
            .startElement "", "", "HTML", Attrs
            .startElement "", "", "HEAD", Attrs 'Auto-emits a META tag for encoding.
            Attrs.addAttribute "", "", "name", "", "generator"
            Attrs.addAttribute "", "", "content", "", App.CompanyName _
                                                    & " " & App.EXEName _
                                                    & " " & CStr(App.Major) _
                                                    & "." & CStr(App.Minor)
            .startElement "", "", "META", Attrs
            Attrs.Clear
            .endElement "", "", "META"
            .startElement "", "", "TITLE", Attrs
            .characters REPORT_TITLE
            .endElement "", "", "TITLE"
            Attrs.addAttribute "", "", "type", "", "text/css"
            .startElement "", "", "STYLE", Attrs
            Attrs.Clear
            .characters CSS_STYLES
            .endElement "", "", "STYLE"
            .endElement "", "", "HEAD"
            .startElement "", "", "BODY", Attrs
            .startElement "", "", "TABLE", Attrs
            .startElement "", "", "TR", Attrs
            For Col = 0 To FieldsUB
                .startElement "", "", "TH", Attrs
                .characters Replace$(Fields(Col).Name, "_", " ")
                .endElement "", "", "TH"
            Next
            .endElement "", "", "TR"
            Do Until RS.EOF
                'Hightlight rows for Southern Hemisphere:
                If Fields(LatitudeField).Value < 0 Then
                    Attrs.addAttribute "", "", "class", "", "SH"
                Else
                    Attrs.Clear
                End If
                .startElement "", "", "TR", Attrs
                    For Col = 0 To FieldsUB
                        .startElement "", "", "TD", Attrs
                        Value = Fields(Col).Value
                        If Not IsNull(Value) Then .characters CStr(Value)
                        .endElement "", "", "TD"
                    Next
                .endElement "", "", "TR"
                RS.MoveNext
            Loop
            .endElement "", "", "TABLE"
            .endElement "", "", "BODY"
            .endElement "", "", "HTML"
            .endDocument
        End With
        With Writer
            .Flush
            If VarType(Dest) = vbString Then
                Dest = .output 'Fetch String output.
            End If
        End With
    End Sub
    The attachment contains some raw data, which is why it is so large.

    MSXML 6.0 has been part of Windows since Vista. You might still be able to download a redist version for XP SP2 or maybe SP3 from Microsoft.
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [VB6] SAX: Not just for XML

    Ahh, here it is:

    Microsoft Core XML Services (MSXML) 6.0

    Supported Operating System

    Windows 2000 Service Pack 4, Windows Server 2003, Windows Server 2003 Service Pack 1, Windows XP Service Pack 1, Windows XP Service Pack 2

    • To install MSXML 6.0 you must have Microsoft Windows® Installer 3.1 (MSI 3.1) or higher on your computer. If you are running Microsoft Windows Server 2003 Service Pack 1 (SP1) you already have the correct version. For other operating systems you may need to upgrade. Please see the Windows Installer 3.1 knowledge base article for details.

  3. #3
    New Member
    Join Date
    Nov 2016
    Location
    Bali, Indonesia
    Posts
    14

    Re: [VB6] SAX: Not just for XML

    Thanks for great work. code running fine even i got a little error using ico.

    i got an error from form1
    Code:
    Line 2: Property Icon in Form1 could not be set.
    try to manually add icon to form1 from properties menu but it say invalid picture.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [VB6] SAX: Not just for XML

    Works fine here but I am using VB6 SP6. Maybe you aren't up to date? I am compiling on Windows 10 1670 so if you are trying to use XP or something that could be part of the problem too.

    But you can always just delete the icon or replace it by another. That one has true-color plus alpha images, so you could strip it down to plain 24-bit or 8-bit images.

  5. #5
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: [VB6] SAX: Not just for XML

    vb6 sp6 cont used 32bpp ico.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [VB6] SAX: Not just for XML

    Handles the icon just fine here. Compiled on Windows 10 1670 (Redstone 1), runs on Windows 7 with no problems.

    Perhaps a VB6 IDE problem on downlevel development platforms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width