Can someone give an XML GetValue example?

mistermentality

Active Member
Licensed User
Longtime User
I have been looking at parsing xml data and I can't seem to find an example on how to use GetName or GetValue and was wondering if someone could post a simple example for newbs like myself?

Dave
 

mistermentality

Active Member
Licensed User
Longtime User
Can you post your XML data?

The data itself is variable because it is from a http search response but I can include a saved part of the format it returns (the different fields, etcetera). I am trying to read the values of all the attributes within each <doc> element and think I need to use GetValue.

The code is formatted as follows (only some of it is posted as it is quite long otherwise):
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<response>
      <lst name="responseHeader">
            <int name="status">0</int>
            <int name="QTime">10</int>
            <lst name="params">
                  <str name="json.wrf">callback</str>
                  <str name="qin">sherlock AND 512kb AND collection:moviesandfilms</str>
                  <str name="fl">description,headerImage,identifier,mediatype,title</str>
                  <str name="indent"/>
                  <str name="start">0</str>
                  <str name="q">(title:sherlock^100 OR description:sherlock^15 OR collection:sherlock^10 OR language:sherlock^10 OR text:sherlock^1) AND (title:512kb^100 OR description:512kb^15 OR collection:512kb^10 OR language:512kb^10 OR text:512kb^1) AND collection:moviesandfilms;</str>
                  <str name="wt">xml</str>
                  <str name="rows">50</str>
            </lst>
      </lst>
      <result name="response" numFound="37" start="0">
            <doc>
                  <str name="description">A collection of theatrical trailers for the Basil Rathbone-Nigel Bruce "Sherlock Holmes" films.</str>
                  <str name="identifier">SherlockHolmesTrailers</str>
                  <str name="mediatype">movies</str>
                  <str name="title">Sherlock Holmes Trailers</str>
            </doc>
            <doc>
                  <str name="description">Based on the Sir Authur Conan Doyle story "The Dancing Men", Sherlock Holmes and Dr. Watson are placed in WWII europe to help protect a scientist and his invention from the Nazis. Basil Rathbone .... Sherlock Holmes Nigel Bruce .... Dr. John H. Watson Lionel Atwill .... Professor Moriarty Kaaren Verne .... Charlotte Eberli William Post Jr. .... Dr. Franz Tobel Dennis Hoey .... Inspector Lestrade Holmes Herbert .... Sir Reginald Bailey Mary Gordon .... Mrs. Hudson</str>
                  <str name="identifier">secret_weapon</str>
                  <str name="mediatype">movies</str>
                  <str name="title">Sherlock Holmes and the Secret Weapon</str>
            </doc>
            <doc>
                  <str name="description">The New Adventures of Sherlock Holmes "The Case of Lady Beryl" Originally aired October 25, 1954. Lady Beryl admits to a crime she did not commit. Holmes not only reveals the true murderer, but Lady Beryl's motive for lying about the crime.</str>
                  <str name="identifier">SherlockHolmes-TheCaseofLadyBeryl</str>
                  <str name="mediatype">movies</str>
                  <str name="title">Sherlock Holmes - The Case of Lady Beryl</str>
            </doc>
            <doc>
                  <str name="description">Failed pilot for a proposed British Sherlock Holmes series starring John Longdon (Hitchcock's "Blackmail" and "The Skin Game") as Sherlock Holmes. Campbell Singer plays Dr Watson and the episode also features Walter Gotell (70s and 80s Bond pictures) as Luzatto. The story is based on the canonical adventure "The Man With The Twisted Lip".</str>
                  <str name="identifier">SherlockHolmes-TheManWhoDisappearedfailedPilot</str>
                  <str name="mediatype">movies</str>
                  <str name="title">Sherlock Holmes - The Man Who Disappeared (Failed Pilot)</str>
            </doc>

I would really appreciate even just a simple example of how to use GetName or GetValue, I have tried guessing how to use it but being new to xml and of course still new to programming in B4A an example would be useful.

Thank you

Dave
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code prints the description and title of each doc:
B4X:
Sub Process_Globals
    Dim parser As SaxParser
    Dim currentName As String
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    parser.Initialize
    Dim in As InputStream
    in = File.OpenInput(File.DirAssets, "test.txt")
    parser.Parse(in, "xml")
    in.Close
End Sub
Sub xml_StartElement (Uri As String, Name As String, Attributes As Attributes)
    'check if we are in 'str' node inside a 'doc' node.
    If Name = "str" AND parser.Parents.IndexOf("doc") > -1 Then
        currentName = Attributes.GetValue2("", "name")
    End If
End Sub

Sub xml_EndElement (Uri As String, Name As String, Text As StringBuilder)
    If Name = "str" AND parser.Parents.IndexOf("doc") > -1 Then
        Select currentName
            Case "description"
                Log("Description: " & Text.ToString)
            Case "title"
                Log("Title: " & Text.ToString)
        End Select
    End If
End Sub
The result:
Description: A collection of theatrical trailers for the Basil Rathbone-Nigel Bruce "Sherlock Holmes" films.

Title: Sherlock Holmes Trailers

Description: Based on the Sir Authur Conan Doyle story "The Dancing Men", Sherlock Holmes and Dr. Watson are placed in WWII europe to help protect a scientist and his invention from the Nazis. Basil Rathbone .... Sherlock Holmes Nigel Bruce .... Dr. John H. Watson Lionel Atwill .... Professor Moriarty Kaaren Verne .... Charlotte Eberli William Post Jr. .... Dr. Franz Tobel Dennis Hoey .... Inspector Lestrade Holmes Herbert .... Sir Reginald Bailey Mary Gordon .... Mrs. Hudson

Title: Sherlock Holmes and the Secret Weapon

Description: The New Adventures of Sherlock Holmes "The Case of Lady Beryl" Originally aired October 25, 1954. Lady Beryl admits to a crime she did not commit. Holmes not only reveals the true murderer, but Lady Beryl's motive for lying about the crime.

Title: Sherlock Holmes - The Case of Lady Beryl

Description: Failed pilot for a proposed British Sherlock Holmes series starring John Longdon (Hitchcock's "Blackmail" and "The Skin Game") as Sherlock Holmes. Campbell Singer plays Dr Watson and the episode also features Walter Gotell (70s and 80s Bond pictures) as Luzatto. The story is based on the canonical adventure "The Man With The Twisted Lip".

Title: Sherlock Holmes - The Man Who Disappeared (Failed Pilot)
 
Upvote 0
Top