B4J Question parser xml

electro179

Active Member
Licensed User
Longtime User
Hello

I use the saxparser

my xml file
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<intallation>
    <cabinet name="Labo 1" ctrl1="2.168.0.11" ctrl2="2.168.0.12">1</cabinet>
    <cabinet name="Labo 2" ctrl1="2.168.0.21" ctrl2="2.168.0.22">2</cabinet>
    <cabinet name="TST" ctrl1="2.168.1.11" ctrl2="2.168.1.12">3</cabinet>
    <cabinet name="office 1" ctrl1="2.168.2.11" ctrl2="2.168.2.12">4</cabinet>
    <cabinet name="office 2" ctrl1="2.168.2.21" ctrl2="2.168.2.22">5</cabinet>
    <cabinet name="office 3" ctrl1="2.168.2.31" ctrl2="2.168.2.32">6</cabinet>
</intallation>

my code for test


B4X:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
   
    If xml.Parents.IndexOf("item") > -1 Then
      If Name = "installation" Then
        Log ("Installation")
      Else If Name = "cabinet" Then
          Log ("cabinet")
      Else If Name = "ctrl" Then
          Log ("ctrl")
      End If
  End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If xml.Parents.IndexOf("item") > -1 Then
      If Name = "installation" Then
        Log ("Installation")
      Else If Name = "cabinet" Then
          Log ("cabinet")
      Else If Name = "ctrl" Then
          Log ("ctrl")
      End If
  End If
End Sub


the Parser_StartElement is called.
the parameters attributes works
but the parameter Name is always empty. Why


thank
 

stevel05

Expert
Licensed User
Longtime User
You don't have any elements in the file named "item" so there will be nothing with that as a parent, neither is there and element called "ctrl", so this won't match either. Also the file has "intallation" and the code has "installation". Change the element in the xml file to "installation" and comment out the parent check and you will get some results.

You can amend it from there. For example:

B4X:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    'If xml.Parents.IndexOf("item") > -1 Then
      If Name = "installation" Then
        Log ("St: Installation")
      Else If Name = "cabinet" Then
          Log ("St: cabinet")
         For i = 0 To Attributes.Size-1
            Log(Attributes.GetName(i) & " " &Attributes.GetValue(i))
         Next
      End If
  'End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    'If xml.Parents.IndexOf("item") > -1 Then
      If Name = "installation" Then
        Log ("End: Installation")
      Else If Name = "cabinet" Then
          Log ("End: cabinet")
      End If
' End If
End Sub

EDIT: to give a better example
 
Last edited:
Upvote 0

electro179

Active Member
Licensed User
Longtime User
always nothing

I tried that

B4X:
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    count = count + 1
    Log(count)
    Log("name :" & Name)
 
End Sub

result :

Program started.
1
name :
2
name :
3
name :
4
name :
5
name :
6
name :
7
name :
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Here's my working example, try that:
 

Attachments

  • saxparser.zip
    1 KB · Views: 257
Upvote 0

coley

Member
Licensed User
Longtime User
Please can someone point me in the right direction.
I admit I know very little about xml files and as a result I have been stuck on this all day.
I'm attempting to get the values from the Rows toward the bottom of the xml file.
Instead I am just getting the Cell names.

Thank you in advance.

Coley

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xml As SaxParser
    Dim TR As TextReader
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    TR.Initialize(File.OpenInput(File.DirAssets,"alarms.xml"))
    xml.Initialize
    xml.Parse2(TR,"Parser")
    MainForm.Show
End Sub
Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
    If xml.Parents.IndexOf("Row") > -1 Then
      If Name = "Cell" Then
        Log ("St: Cell")
        For i = 0 To Attributes.Size-1
            Log(Attributes.GetName(i) & " " &Attributes.GetValue(i))
        Next
      End If
  End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If xml.Parents.IndexOf("Row") > -1 Then
      If Name = "Cell" Then
          Log ("End: Cell")
      End If
    End If
End Sub

I get the following (snipped for brevity)

B4X:
Program started.
St: Cell
name InstanceID
End: Cell
St: Cell
name Guid
End: Cell
St: Cell
name TriggerTime
End: Cell
St: Cell
name TriggerEntity
End: Cell
St: Cell
name TriggerEvent
End: Cell
St: Cell
name CreationTime
End: Cell
St: Cell
name AckTime
End: Cell
St: Cell
name AckBy
End: Cell
St: Cell
name AckReason
End: Cell
St: Cell
name ExternalInstanceID
End: Cell
St: Cell
name OfflinePeriod
End: Cell
 

Attachments

  • alarms.zip
    681 bytes · Views: 237
Upvote 0
Top