User Tools

Site Tools


k27:k27.3:start

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 27.3.1.1: 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
  xmlReader.Open(sXMLPath)
    For Each sLine In XmlReader.InputStream.Lines
      txaXML.Insert(sLine & gb.NewLine)
    Next
  xmlReader.Close()
 
  txaXML.Pos = 0
 
End

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 https://gambas-playground.proko.eu/?gist=b90eeff3dacbec0548d01701f3c05133.

27.3.1.1.1 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 27.3.1.1.1.1: 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

27.3.1.2 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.
NameInteger
ValueVariantReturns the value of the current attribute.

Table 27.3.1.2.1: 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 27.3.2.1: Selected methods of the class XmlReader

27.3.3.3 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:

XML-Reader
Figure 27.3.3.1: 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()
  ShowXMLContent()
  txaXML.Pos = 0
  HSplit1.Layout = [3, 2]
End
 
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)
    txaList.Insert(gb.NewLine)
  Next
 
  File.Save(Application.Path &/ "files/addresslist.txt", txaList.Text)
End
 
Private Sub ShowXMLContent()
 
  Dim sLine As String
 
  hXMLReader = New XmlReader
  hXMLReader.Open(sXMLPath)
    For Each sLine In hXMLReader.InputStream.Lines
      txaXML.Insert(sLine & gb.NewLine)
    Next
  hXMLReader.Close()
 
End
 
Private Function GetRecords() As Variant[]
 
  Dim asRecord As String[]
  Dim avRecords As New Variant[]
 
  txaList.Clear()
  asRecord = New String[]
 
  hXMLReader = New XmlReader
  hXMLReader.Open(sXMLPath)
  hXMLReader.Read()
 
  While Not hXMLReader.Eof
    Select Case hXmlReader.Node.Type
      Case XMLReaderNodeType.Element
        If hXmlReader.Node.Name = "mw" Then
           hXMLReader.Read()
           If hXmlReader.Node.Value = "w" Then
              asRecord.Add("Frau", 0)
           Else
              asRecord.Add("Herr", 0)
           Endif
           avRecords.Add(asRecord)
           asRecord = New String[]
        Endif
        If hXmlReader.Node.Name = "firstname" Then
           hXMLReader.Read()
           asRecord.Add(hXmlReader.Node.Value, 1)
        Endif
        If hXmlReader.Node.Name = "surname" Then
           hXMLReader.Read()
           asRecord.Add(hXmlReader.Node.Value, 2)
        Endif
        If hXmlReader.Node.Name = "street" Then
           hXMLReader.Read()
           asRecord.Add(hXmlReader.Node.Value, 3)
        Endif
        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
           Next
           hXMLReader.Read()
           asRecord.Add(hXmlReader.Node.Value, 5)
        Endif
    End Select
    hXMLReader.Read()
  Wend
  hXMLReader.Close()
 
  Return avRecords
 
End

Download

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