Android Question Simple XML parser

Dario126

Member
Licensed User
Longtime User
I'm aware of XmlSax parser, but it's overkill for my purpose, I don't like unnecessary non-linear programming, and most important it can not fit into my code module where I have my most common functions... in other words, not tidy enough :=)

I have simple XML-s (meaning no hierarchy structure), and need to extract data to map.

Is there such "converter" existing in someones repository already?
 

Dario126

Member
Licensed User
Longtime User
Uh, also looks "overkill" for what I need ..

In the meantime I have done something like this, and it works well (except I need to implement &lt and &gt) to get any parameter from XML string by giving key string of known node:

B4X:
Sub GetParamFromXML(sXML As String, ParameterName As String) As String
Dim i1, i2 As Int
Dim t1, t2 As String

    'LOCATE START AND END
    t1 = "<" & ParameterName & ">"
    t2 = "</" & ParameterName & ">"
    i1 = sXML.IndexOf(t1)
    i2 = sXML.IndexOf(t2)

    'MSG IF NOT FOUND
    If i1<0 Then
        LogColor("Not found in XML: " & ParameterName, Colors.Magenta)
        ToastMessageShow("Not found in XML: " & ParameterName, False)
        Return("0")
    End If

    LogColor("From XML: " & ParameterName & " = " & sXML.SubString2(i1+t1.Length, i2), Colors.Blue)
   
    'RESULT
    Return(sXML.SubString2(i1+t1.Length, i2))
   
End Sub

Now I just need to find out how to get list of all nodes from XML, and extract one by one.

I know it's not optimized for speed, but I have very small XML files ..
Another idea was to run through XML by searching each <>, and extract node name and value .. that would be better, but function above was also needed for me ..
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
Back to this topic. I made one function to copy all data from SIMPLE XML to map as shown in example. Note that this one expect MS Access header ("dataroot" node), but it's easy to change if necessary. This works fine for me in this case ..

B4X:
Sub XML_WriteXmlDataToMap(sXML As String, iDataMap As Map, PreEraseMap As Boolean)
'FUNCTION WHICH TAKES SIMPLE XML STRING
'AND WRITE IT TO MAP
'
'NOTE: AJDUSTED FOR MS ACCESS SCHEMA


    Dim id1, id2 As Int        'data area start (id1) and end (id2) marked by "dataroot" tag
    Dim it1, it2 As Int        'position of start tag
    Dim it3, it4 As Int        'position of end tag
    Dim i As Int            'cursor position
    Dim sKey, sValue As String

    'CLEAR MAP IF REQUESTED
    If PreEraseMap Then iDataMap.Clear

    'LOCATE START (id1) AND END (id2) OF DATA .. marked with "dataroot" for MS Access schema
    id1 = sXML.IndexOf("<dataroot")
    id1 = sXML.IndexOf2(">", id1) + 1
    id2 = sXML.IndexOf("</dataroot>") - 1

    'LOOP FROM id1 TO id2
    i = id1
    Do While i < id2
        it1 = sXML.IndexOf2("<", i)                'find start pos of start tag
        it2 = sXML.IndexOf2(">", it1)            'find end pos of start tag
        sKey = sXML.SubString2(it1+1, it2)        'take between them - key name
        it3 = sXML.IndexOf2("</", it2)            'find start pos of end tag
        it4 = sXML.IndexOf2(">", it3)            'find end pos of end tag
        sValue = sXML.SubString2(it2+1, it3)
        sValue = sValue.Replace("&lt;","<").Replace("&gt;",">")
        iDataMap.Put(sKey, sValue)
        i = it4+1                                'move cursor to new pos
    Loop
      
End Sub
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
My reason is simplicity of use in main code (copy xml->map with one line), no external libraries (simple reuse), etc.

In my case it works just fine because I also create input XML's and I know they are correct and correctly structured. For other cases (another sources) there should be some error checking.

What could I get better with XOM (probably by mistake You write DOM)??

Again: I know I have only FLAT simple XML's (in my case predefined for use in MS Access) ..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…