User Tools

Site Tools


27.3 Class XmlReader

The class XmlReader (gb. xml) represents an XML stream reader that implements the SAX API. This class inherits the virtual class _XmlReader.

You can create an object of the class XmlReader (gb. xml):

Dim hXmlReader As XmlReader
hXmlReader = New XmlReader()

27.3.1 Properties

Essential properties of the class XmlReader:

PropertyData typeDescription
DepthIntegerSpecifies the search depth with respect to the currently read node relative to the XML tree.
EofBooleanReturns True when XmlReader. Read () reaches the end of the XML file.
Node .XmlReader.nodeReturns information about the currently read node. The properties of the virtual class XmlReader. node are used.
PosIntegerReturns the position in the XML stream where the internal pointer is located.
ReadFlags .XmlReaderReadFlagsReturns a virtual class that can be used like a read/write array.
StoredNodesXmlNode[]Returns an array with all nodes in the XML stream.
InputStreamStreamXmlReader.InputStream.Lines returns a virtual object that allows you to iterate through a stream line by line.

Table Properties of the class XmlReader

The following example shows how you can easily display the content of an XML file in a TextArea:

Public xmlReader As XmlReader
Public sXMLPath As String = "files/example.xml"
Public Sub Form_Open()
  Dim sLine As String
  xmlReader = New XmlReader
    For Each sLine In XmlReader.InputStream.Lines
      txaXML.Insert(sLine & gb.NewLine)
  txaXML.Pos = 0

If you are only interested in the attributes when reading an XML file sequentially, you can hide selected node types such as elements, end element, and text in this way:

XmlReader.ReadFlags[XmlReaderNodeType.Attribute] = True
XmlReader.ReadFlags[XmlReaderNodeType.Element] = False
XmlReader.ReadFlags[XmlReaderNodeType.Text] = False
XmlReader.ReadFlags[XmlReaderNodeType.EndElement] = False

Adrien Prokopowicz has provided a suitable interactive example at Properties of the virtual Class XmlReader.Node

Here you will find an overview of the properties of the virtual class XmlReader.Node

PropertyData typeDescription
IsEmptyElementBooleanReturns True if the item is empty.
NameStringReturns the name of the current attribute node.
TypeIntegerReturns the type of the current attribute node.
ValueStringReturns the value of the current attribute node.
Attributes. XmlReader. Node. AttributesReturns a virtual collection of all attribute nodes of the current node.

Table Properties of the class XmlReader.Node

The class XmlReader.Node behaves like a read-only array:

Dim hXmlReader.Node As XmlReader.Node
Dim sString As String
sString = hXmlReader.Node.Attributes[Name As String]

and is enumerated using a For-Each control structure:

Dim hXmlReader.Node As XmlReader.Node
Dim sString As String
For Each sString In hXmlReader.Node.Attributes
Next Properties of the virtual class XmlReader.Node.Attributes

The virtual class XmlReader.Node.Attributes has three properties:

PropertyData typeDescription
CountIntegerReturns the number of attributes for the current node.
ValueVariantReturns the value of the current attribute.

Table Properties of the class XmlReader.Node.Attributes

The class XmlReader.Node.Attributes has only one method:

Function Exist(Name As String) As Boolean

The function returns True if an attribute with the specified name' Name' exists for the current node.

The class XmlReaderNodeType is static and contains only constants:

  • Attributes (8),
  • CDATA (4),
  • Comment (3),
  • Document (0),
  • DocumentFragment (0),
  • DocumentType (0),
  • Element (0),
  • EndElement (6),
  • EndEntity (0),
  • EndEntity (0),
  • EndStream (7),
  • Entity (0),
  • EntityReference (0),
  • EntityReference (0),
  • None (0), Notation (0),
  • ProcessingInstruction (0),
  • SignificantWhitespace (0),
  • Text (2), Whitespace (0),
  • XmlDeclaration (0)

27.3.2 Methods

Here you will find an overview of the four methods of the class XmlReader:

MethodReturn typeDescription
Open (path As String)-Opens an XML file with the specified path path.
Close ()-Close a file opened with the Open () method.
FromString (Content As String)- Overwrites the content of an XmlReader object with the content of' Content'.
Read ()IntegerReads and returns a node.

Table Selected methods of the class XmlReader Example XML parser (XmlReader)

In the presented example, selected data from contact data - stored in an XML file - is to be read out and then displayed in a TextArea. The data read out, prepared and saved in a text file could, for example, serve as the basis for printing address labels:

Figure GUI of the sample project' XML-Parser'.

The source code is completely specified, pay special attention to the source code of the function GetRecords ():

' Gambas class file
Public hXMLReader As XmlReader
Public sXMLPath As String = "files/list.xml"
Public Sub Form_Open()
  txaXML.Pos = 0
  HSplit1.Layout = [3, 2]
Public Sub btnShowRecords_Click()
  Dim i, j As Integer
  Dim asMatrix As String[]
  Dim avRecords As New Variant[]
  Dim sSpace As String = String$(4, " ")
  avRecords = GetRecords()
  For i = 0 To avRecords.Max
    asMatrix = New String[]
    asMatrix = avRecords[i]
    txaList.Insert(sSpace & asMatrix[0] & gb.NewLine)
    txaList.Insert(sSpace & asMatrix[1] & " " & asMatrix[2] & gb.NewLine)
    txaList.Insert(sSpace & asMatrix[3] & gb.NewLine)
    txaList.Insert(sSpace & asMatrix[4] & " " & asMatrix[5] & gb.NewLine)
  File.Save(Application.Path &/ "files/addresslist.txt", txaList.Text)
Private Sub ShowXMLContent()
  Dim sLine As String
  hXMLReader = New XmlReader
    For Each sLine In hXMLReader.InputStream.Lines
      txaXML.Insert(sLine & gb.NewLine)
Private Function GetRecords() As Variant[]
  Dim asRecord As String[]
  Dim avRecords As New Variant[]
  asRecord = New String[]
  hXMLReader = New XmlReader
  While Not hXMLReader.Eof
    Select Case hXmlReader.Node.Type
      Case XMLReaderNodeType.Element
        If hXmlReader.Node.Name = "mw" Then
           If hXmlReader.Node.Value = "w" Then
              asRecord.Add("Frau", 0)
              asRecord.Add("Herr", 0)
           asRecord = New String[]
        If hXmlReader.Node.Name = "firstname" Then
           asRecord.Add(hXmlReader.Node.Value, 1)
        If hXmlReader.Node.Name = "surname" Then
           asRecord.Add(hXmlReader.Node.Value, 2)
        If hXmlReader.Node.Name = "street" Then
           asRecord.Add(hXmlReader.Node.Value, 3)
        If hXmlReader.Node.Name = "residence" Then
           For Each hXmlReader.Node.Attributes ' Zuerst die Attribute auslesen!
             Select Case hXmlReader.Node.Attributes.Name
               Case "plz"
                asRecord.Add(hXmlReader.Node.Attributes.Value, 4)
             End Select
           asRecord.Add(hXmlReader.Node.Value, 5)
    End Select
  Return avRecords


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k27/k27.3/start.txt · Last modified: 25.04.2022 (external edit)

Page Tools