Android Question Read XML file

jcmartini

Member
Licensed User
Longtime User
I wanted to read a simple xml file. I tried xmlsax with the following code:

Sub Parser_StartElement (Uri As String, Name As String, Attributes As Attributes)
Dim idx, Caption As String
'If Name = "Node" Then ' Begin @ idx=3
'If Name = "Items" Then ' Shows nothing
'If parser.Parents.IndexOf("Items") > -1 Then ' Begin @ idx=3
idx = Attributes.GetValue2("", "idx") ' Begin @ idx=1
Caption = Attributes.GetValue2("", "Caption")
ListView1.AddSingleLine(idx & " : " & Caption)
'End If
End Sub
Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
End Sub

I cannot see the first zero idx. How should I do to have that ?

Another question: Where could I find XmlSax version 10 ?


1790935665040.png
 

Attachments

  • pizzas_andr.xml
    1,004 bytes · Views: 53

Chris2

Well-Known Member
Licensed User
Longtime User
You should use CODE /CODE tags when posting code. It makes it much easier to read.

I realise that I'm not really answering your quesiton directly, but it might be easier for you to switch to Xml2Map:
 
Upvote 3

jcmartini

Member
Licensed User
Longtime User
I saw a lot of posts which indicates that people has a lot of difficulties to solve their problems. Could you give me a simple example working with attributes ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tips:
1. Use the GetElements sub from that post: https://www.b4x.com/android/forum/threads/b4x-xml2map-simple-way-to-parse-xml-documents.74848/
In retrospect it should have been part of the library.

2. The first thing that I like to do when parsing XML is to convert it to json and print it. It is simpler than it might sound:
B4X:
Dim m As Map = xm.Parse(...)
Log(m.As(Json).ToString)

It helps understanding the structure.

About attributes:
B4X:
If m.ContainsKey("Attributes") Then
 Dim attr As Map = m.Get("Attributes")
Log(attr)
 
Upvote 0
Top