Sax Parser to list Redundancy

CharlesIPTI

Active Member
Licensed User
Longtime User
REF::::

<ArrayOfCLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<CLocation>
<DBID>75852</DBID>
<LocationName>0103E01A1</LocationName>
<SKU />
<Shelf>1</Shelf>
<Orientation>0</Orientation>
<Short>false</Short>
<Out>false</Out>
<WalkSequence>0</WalkSequence>
</CLocation>
<CLocation>
<DBID>75853</DBID>
<LocationName>0103E01A2</LocationName>
<SKU>F9719-AF1A</SKU>
<Shelf>1</Shelf>
<Orientation>0</Orientation>
<Short>false</Short>
<Out>false</Out>
<WalkSequence>0</WalkSequence>
</CLocation>
</ArrayOfCLocation>




B4X:
Sub getCartLocParser_StartElement(Uri As String, Name As String, Attributes As Attributes)
                  
                  
         If Name = "CLocation" Then                              
               Try                                             
                     daObject.mapLocInst.Initialize    ' New Object Reference                                                                                                                  
               Catch               
                     Msgbox(LastException.Message, "ERROR")                                                      
               End Try               
            End If
            

                                             
End Sub


Sub getCartLocParser_EndElement (Uri As String, Name As String, Text As StringBuilder)

   Dim strRetVal  As String
   Dim strParent As String
   Dim strDBId As String






      If Name = "DBID" Then                              
         Try                                                   
               daObject.mapLocInst.dbId = Text   
               'localMapLocObj.dbId = Text
               sbSanity.Append("Name: " &  Name & "Value: " & Text & "First Node" & CRLF)                        
         Catch
               daObject.mapLocInst.dbId = 0
         End Try               
      End If
                        
      If Name = "LocationName" Then                              
         Try
               daObject.mapLocInst.locNm = Text
         Catch
               daObject.mapLocInst.locNm = ""                        
         End Try
      End If
      
         If Name = "SKU" Then                              
         Try
               daObject.mapLocInst.sku = Text
         Catch
               daObject.mapLocInst.sku = ""
         End Try               
      End If
      
      If Name = "Shelf" Then                              
         Try
               daObject.mapLocInst.shlf = Text
         Catch
               daObject.mapLocInst.shlf = 0                        
         End Try
      End If
      
      If Name = "Orientation" Then                              
         Try                                                   
               daObject.mapLocInst.ori = Text
         Catch
               daObject.mapLocInst.ori = 0
         End Try               
      End If   
      
      If Name = "Short" Then                              
         Try
               daObject.mapLocInst.sho = Text
         Catch
               daObject.mapLocInst.sho = 0                        
         End Try
      End If
      
      If Name = "Out" Then                              
         Try                                                   
               daObject.mapLocInst.ot = Text
         Catch
               daObject.mapLocInst.ot = 0
         End Try               
      End If                        
         
      If Name = "WalkSequence" Then                              
         Try                                                   
               daObject.mapLocInst.wlkSeq = Text
         Catch
               daObject.mapLocInst.wlkSeq = 0
         End Try               
      End If
                                                
      If Name = "CLocation" Then                              
         Try            
         
         Dim cTmpLoc As CLocation
         cTmpLoc = daObject.mapLocInst
               mapLocList.Add(cTmpLoc)
               'daObject.mapLocInst = Null
               sbSanity.Append("Adding Object ID:" & daObject.mapLocInst.dbId & CRLF)
         Catch               
               Msgbox("CLocation List Fail", "Fail")                                                      
         End Try
         
      End If

      If Name = "ArrayOfCLocation" Then                                       
      Msgbox(sbSanity.ToString, "RESULT")          
         Try
               If mapLocList.Size > 0  Then
            bMapLocationsFound = True                                    
            End If 
         Catch               
               Msgbox("bMapLocationsFound Boolean Flag Fail", " bMapLocationsFound Fail")                                                      
         End Try
         
      End If
      

End Sub



PLEASE ANYONE How do I correctly add the newly filled
cTmpLoc OR daObject.mapLocInst to the list
mapLocList.Add

Instead of adding 268 instance of the same data to the list. My string builder says my objects being added to the list are fine. but actually they are not
SEE Attachment Image its like the object (type) gets added to the list but every added instance gets updated to the data reflecting the content of newest addition. So in effect the list ends up holding 268 instances of the last object
while the string builder shows 268 unique instances :sign0085::sign0085:
 

Attachments

  • Capture.JPG
    36.9 KB · Views: 171

CharlesIPTI

Active Member
Licensed User
Longtime User
CTmpLoc

No

CTmpLoc was just an emergency test
so please don't recommend initializing it.

It wont help
If you initialize with every new node / Start Element named"CLocation"
daObject.mapLocInst.Initialize

Then Shouldn't I have a fresh NEW instance to throw into the list ???

I cant see what this is updating every instance placed into the list.
What is the trick to make the new instance and place IT in the list instead of a reference to the new object ??
 
Upvote 0

CharlesIPTI

Active Member
Licensed User
Longtime User
Answer

So I of course make a little test project in these situations and they always work as assumed. So I look REAL HARD at the limitations and scope of the little test I mocked up and ----->

You need a global instance of the CLocation so It can be seen in the end element sub but for clarity and to assure that your using a new specific instance of the CLocation type to fill & add to the list , we set the used one (just added to the list) to Null and we re-dim & it in the start element .

Honestly I don't see a spot where this type is being initialized EVER for this little bit of functionality


But this gets the job done nicely -- That is the list filled ..
It instinctively just feels wrong to declare this more than once " Dim localMapLocObj As CLocation"

but this is what creates a new instance of the type once you've set the original to null.

B4X:
Sub Globals
   Dim localMapLocObj As CLocation
End Sub

B4X:
Sub getCartLocParser_StartElement(ByVal Uri As String, ByVal Name As String, ByVal Attributes As Attributes)


    If Name = "CLocation" Then
        Try
            '   daObject.mapLocInst.Initialize    ' New Object Reference
            Dim localMapLocObj As CLocation
        Catch
            Msgbox(LastException.Message, "ERROR")
        End Try
    End If
End Sub


Sub getCartLocParser_EndElement(ByVal Uri As String, ByVal Name As String, ByVal Text As StringBuilder)

    Dim strRetVal As String
    Dim strParent As String
    Dim strDBId As String

    If Name = "DBID" Then
        '---------------Do Stuff
    End If

    If Name = "LocationName" Then
        '---------------Do Stuff
    End If

    If Name = "SKU" Then
        '---------------Do Stuff
    End If

    If Name = "Shelf" Then
        '---------------Do Stuff
    End If

    If Name = "Orientation" Then
        '---------------Do Stuff
    End If

    If Name = "Short" Then
        '---------------Do Stuff
    End If

    If Name = "Out" Then
        '---------------Do Stuff
    End If

    If Name = "WalkSequence" Then
        '---------------Do Stuff
    End If

    If Name = "CLocation" Then
        Try
            mapLocList.Add(localMapLocObj)
            localMapLocObj = Null
        Catch
            Msgbox("CLocation List Fail", "Fail")
        End Try
    End If
    If Name = "ArrayOfCLocation" Then
        'Msgbox(sbSanity.ToString, "RESULT")          
        Try
            If mapLocList.Size > 0 Then
                bMapLocationsFound = True
            End If
        Catch
            Msgbox("bMapLocationsFound Boolean Flag Fail", " bMapLocationsFound Fail")
        End Try
    End If

End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…