B4J Question [Solved] Facing issue while parsing XML file

AndroidMadhu

Active Member
Licensed User
Hello ,

I am trying to parse one xml file. I am able to get the book details [with Author, Name, Category etc].
But I am not able to get the attributes [ID] of that book.. Here is my code
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xmlparser As Map
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    Dim xm As Xml2Map
    xm.Initialize
    xmlparser=xm.Parse(File.ReadString(File.DirAssets,"books.xml"))
    Dim catalog As Map=xmlparser.Get("catalog")
    Dim book As List=catalog.Get("book")
    For Each bookdet As Map In book
        Dim authname As String=bookdet.Get("author")
        Dim price As String=bookdet.Get("price")
        Dim genre As String=bookdet.Get("genre")
        Log("The Author is : " & authname & " Price is : " & price & " , Catagory is : " & genre )
'        Dim attr As Map=bookdet.Get("Attributes")   ---> Here I am getting error
'        For Each attrid As List In attr
'            Dim id As String=attrid.Get("id")
'            Log(id)
'        Next
    Next
'    Dim jg As JSONGenerator
'    jg.Initialize(xmlparser)
'    Log(jg.ToPrettyString(4))
End Sub

Attached is the xml file...

Please advice on this

Thanks
 

Attachments

  • books.xml
    4.4 KB · Views: 45

AndroidMadhu

Active Member
Licensed User
Hello ,

I am trying to parse one xml file. I am able to get the book details [with Author, Name, Category etc].
But I am not able to get the attributes [ID] of that book.. Here is my code
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xmlparser As Map
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    Dim xm As Xml2Map
    xm.Initialize
    xmlparser=xm.Parse(File.ReadString(File.DirAssets,"books.xml"))
    Dim catalog As Map=xmlparser.Get("catalog")
    Dim book As List=catalog.Get("book")
    For Each bookdet As Map In book
        Dim authname As String=bookdet.Get("author")
        Dim price As String=bookdet.Get("price")
        Dim genre As String=bookdet.Get("genre")
        Log("The Author is : " & authname & " Price is : " & price & " , Catagory is : " & genre )
'        Dim attr As Map=bookdet.Get("Attributes")   ---> Here I am getting error
'        For Each attrid As List In attr
'            Dim id As String=attrid.Get("id")
'            Log(id)
'        Next
    Next
'    Dim jg As JSONGenerator
'    jg.Initialize(xmlparser)
'    Log(jg.ToPrettyString(4))
End Sub

Attached is the xml file...

Please advice on this

Thanks
Fixed that one.....

Workable code piece:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xmlparser As Map
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    Dim xm As Xml2Map
    xm.Initialize
    xmlparser=xm.Parse(File.ReadString(File.DirAssets,"books.xml"))
    Dim catalog As Map=xmlparser.Get("catalog")
    Dim book As List=catalog.Get("book")
    For Each bookdet As Map In book
'        Dim authname As String=bookdet.Get("author")
'        Dim price As String=bookdet.Get("price")
'        Dim genre As String=bookdet.Get("genre")
'        Log("The Author is : " & authname & " Price is : " & price & " , Catagory is : " & genre )
        Dim attr As Map=bookdet.Get("Attributes")
        Dim id As String=attr.Get("id")
        Dim authname As String=bookdet.Get("author")
        Dim price As String=bookdet.Get("price")
        Dim genre As String=bookdet.Get("genre")
        Log("Book ID: " & id & " Author is : " & authname & " Price is : " & price & " , Catagory is : " & genre )   
    Next
'    Dim jg As JSONGenerator
'    jg.Initialize(xmlparser)
'    Log(jg.ToPrettyString(4))
End Sub

Thanks
 
Upvote 0
Top