iOS Question How do I read the attribute values of an xml element using sax parser

davepamn

Active Member
Licensed User
I need to read the attribute values of an xml element

B4X:
Dim oList As List

oList.Initialize
Dim In As InputStream

    In = File.OpenInput(File.DirAssets, "xml_fh378692.xml")
    parser.Parse(In, "Parser")

    For i=0 To oList.Size-1
        Log(oList.Get(i))
    Next

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Map)

    Dim sElement_Name As String
    Dim sPixelTop As String
    Dim sPixelLeft As String

    If Name="node" Then

        sElement_Name=Attributes.Get("element_name")
        sPixelTop=Attributes.Get("pixelTop")
        sPixelLeft=Attributes.Get("pixelLeft")
        oList.Add(sElement_Name & " top:" & sPixelTop & " left:" & sPixelLeft)

     End If

End Sub

I can see the attributes as the parser walks down the xml structure.

I don't understand why Text displays the same value when the node structure is nested.
[code]
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)

    If parser.Parents.IndexOf("node") =1 Then

        If Name = "node" Then

            Log(Text.ToString)

        EndIf

    EndIf

End Sub
XML Code

B4X:
<node element_name="Positional Div Parent" key="DIV6" parent_key="root" name="tblTABLE6" type="TABLE" pixelTop="330" pixelLeft="30" border="2" width="370">

<node element_name="Table Header" key="tblTABLE6_head" parent_key="DIV6">
<node element_name="Table Body" key="tblTABLE6_body" parent_key="DIV6">
<node element_name="Table Row" key="tblTABLE6_body_row1" parent_key="tblTABLE6_body">

<node element_name="na" key="tblTABLE6_body_cell1" parent_key="tblTABLE6_body_row1" backgroundColor="#ffffff" width="350" align="left" height="25" valign="top"><node ctype="cvalue"><![CDATA[This is the data that is repeating in the End Element.  How do I get around the nesting problem?]]></node>
</node>
</node>
</node>
</node>
 
Last edited:

davepamn

Active Member
Licensed User
B4X:
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)

        If Name = "node" Then

            oList.Add(Text.tostring)

            Text.Remove(0,Text.Length)

        EndIf

End Sub
 
Top