BCXML.dll


BCXML.dll is a non-validating XML parser, implemented as an ActiveX code component and written in VB6. It is intended as a replacement for the Microsoft XML parser, MSXMLn.dll. It is simpler, more logical, and more intuitive than the Microsoft parser, especially the SAX parser.

To install it, copy BCXML.dll to the Windows System folder and register it (using regsvr32.exe or other method).

BCXML contains two Class definitions:

CXMLDocument

Properties & methods

  • FileName                (String) Sets or returns full path to XML file.
  • Root                    (CXMLElement) Not available until Load is called.
  • Load()                  Loads model from XML file into memory. 
                            You get to the model via the Root property.
  • Parse()                 Does a SAX style parse. You must implement
                            BeginElement and EndElement event handlers,
                            but you do not need to implement an interface
                            like you do with SAX.
  • Save([filename])        Writes to disk. 

Events
  • BeginElement(CXMLElement) Raised when element's open-tag is encountered
  • EndElement(CXMLElement)   Raised when end-tag or "/>" isa encountered.

CXMLElement

Properties & methods
  • Name                      (String) Name of element, from XML tag
  • Attributes(AttribName)    Sets or returns attribute value.
                              Assignment will create new attribute if not exists.
  • Text                      (String) Sets or returns XML text.
  • Parent                    (CXMLElement) Returns ref to parent element (Nothing for Root)
  • FirstChild()              (CXMLElement) Returns Nothing if no children.
  • NextChild()               (CXMLElement) Returns Nothing if no more children.
  • AddChild(CXMLElement) 
  • Delete                    Deletes the element (Not yet implemented).

For iterating over all attributes, these are provides:
  • AtrributeCount            (Integer) The number of attributes.
  • AttributeName(i)          (String) Returns the i-th attribute name (0 based)
  • AttributeValue(i)         (String) Returns the i-th attribute value (0 based)

For iterating over all child elements, these are provides:
This is an alternative to using FirstChild and NextChild.
  • ChildCount                (Integer) The number of attributes.
  • Children(i)               (CXMLElement) Sets or returns the i-th child element (0 based)

Example 1 (VB6) - Load XML into memory

Here is how you could open an XML file and load it into memory and then process the loaded model...
Sub ProcessXML()

   Dim doc As CXMLDocument

   Set doc = New CXMLDocument
   doc.FileName = "c:\mystuff\myxml.xml"
   doc.Load

'  To process every element...
   ProceesOneElement doc.Root 

End Sub


Sub ProcessOneEelment(elt As CXMLElement)

   Dim e As CXMLElement

'  Stuff you want to do with this element goes here...
   Debug.Print elt.Name & " " & elt.Attributes("MyAttrib")
   Debug.Print elt.Text

'  Now process all sub-elements of this element...
   Set e = elt.FirstChild
   Do Until e Is Nothing
      ProcessOneElement e
      Set e = elt.NextChild
   Loop

End Sub

Example 2 (VB6) - SAX-like parsing

Here is how you could do the same thing without loading the model, but rather using events as the file is read. You must implement BeginElement and EndElement event handler methods. This is like using SAX.
Sub ProcessXML()

   Dim WithEvents doc As CXMLDocument

   Set doc = New CXMLDocument
   doc.FileName = "c:\mystuff\myxml.xml"
   doc.Parse  'Event handlers (below) will be invoked

End Sub


Sub doc_BeginElement(elt As CXMLElement)

   Debug.Print elt.Name & " " & elt.Attributes("MyAttrib")

End Sub


Sub doc_EndElement(elt As CXMLElement)

   Debug.Print elt.Name & " " & elt.Text

End Sub

Example 3 (VB6) - Iterate over all attributes

Here is how you could iterate over all attributes of an element, without knowing their names beforehand.
Sub ShowAttributes(e As CXMLElement)

   Dim i As Integer
   For i = 0 To e.AttributeCount - 1
      Debug.Print e.AttributeNames(i) & " = " & e.AttributeValues(i)
   Next
   
End Sub

End Sub