XML Parsing in VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

As long as the XML conforms to XML Syntax then it is easy enough to parse with a Visual BASIC application.

You can code your own parser or use an existing XML parser. It is recommended that you use a parser that supports the XML Document Object Model (DOM).

Microsoft XML Parser (Msxml.dll)

Microsoft's implementation of the DOM fully supports the W3C standard and has additional features that make it easier for you to work with XML files from your programs. Msxml.dll contains the type library and implementation code via a set of standard COM interfaces.

Set objParser = CreateObject( "Microsoft.XMLDOM" )

If you're working with Visual Basic, you can access the DOM by setting a reference to the MSXML type library, provided in Msxml.dll.

VB6 References: Microsoft XML, version 2.0

Dim xDoc As MSXML.DOMDocument 
Set xDoc = New MSXML.DOMDocument

To load an XML document, create an instance of the DOMDocument class:

Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
If xDoc.Load("C:\My Documents\cds.xml") Then

Do this if it loads

Else

If it fails to load

End If

Set xDoc = Nothing

ReadyState Properties: State Value

0 = Uninitialized: loading has not started.
1 = Loading: while the load method is executing.
2 = Loaded: load method is complete.
3 = Interactive: enough of the DOM is available for read-only examination and the data has only been partially parsed.
4 = Completed: data is loaded and parsed and available for read/write operations.

loading a file from a URL example:

xDoc.async = False
If xDoc.Load("http://www.blah.com/blah.xml") Then
  blah blah
Else
  blah blah
End If

By setting the document's Async property to False, the parser will not return control to your code until the document is completely loaded and ready for manipulation. If you leave it set to True, you will need to either examine the ReadyState property before accessing the document or use the DOMDocument's events to have your code notified when the document is ready.