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()
Essential properties of the class XmlReader:
Property | Data type | Description |
---|---|---|
Depth | Integer | Specifies the search depth with respect to the currently read node relative to the XML tree. |
Eof | Boolean | Returns True when XmlReader. Read () reaches the end of the XML file. |
Node | .XmlReader.node | Returns information about the currently read node. The properties of the virtual class XmlReader. node are used. |
Pos | Integer | Returns the position in the XML stream where the internal pointer is located. |
ReadFlags | .XmlReaderReadFlags | Returns a virtual class that can be used like a read/write array. |
StoredNodes | XmlNode[] | Returns an array with all nodes in the XML stream. |
InputStream | Stream | XmlReader.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.
Here you will find an overview of the properties of the virtual class XmlReader.Node
Property | Data type | Description |
---|---|---|
IsEmptyElement | Boolean | Returns True if the item is empty. |
Name | String | Returns the name of the current attribute node. |
Type | Integer | Returns the type of the current attribute node. |
Value | String | Returns the value of the current attribute node. |
Attributes | . XmlReader. Node. Attributes | Returns 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
The virtual class XmlReader.Node.Attributes has three properties:
Property | Data type | Description |
---|---|---|
Count | Integer | Returns the number of attributes for the current node. |
Name | Integer | |
Value | Variant | Returns 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:
Here you will find an overview of the four methods of the class XmlReader:
Method | Return type | Description |
---|---|---|
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 () | Integer | Reads and returns a node. |
Table 27.3.2.1: Selected methods of the class 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 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