B4J Library MashXML

Ola

MashXML seeks to be an extension of Erel's XML2Map module with special attention to Library XMLs. I did this to help me parse library xml's for other tasks I needed.

MashXML

Author: Anele Mashy Mbanga
Version: 1
  • MashXML
    • Functions:
      • Process_Globals As String
      • Xml2JSON (sPath As String) As String
        returns xml file data as a json string, pass the complete file path
      • XmlToMap (sPath As String) As Map
        returns a map with classes and project keys
        each classes keys are classname,description,properties,methods,fields and events
        each properties,methods,fields,events is a list of maps with attributes for that class
        properties & fields - keyname,fieldtype,defaultvalue,description
        methods & events - keyname,fieldtype,description,parameterdef,parameters
        project returns a map with - libraryname,author,version
  • Xml2Map
    • Fields:
      • elements As List
    • Functions:
      • Class_Globals As String
      • Initialize As String
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • Parse (XML As String) As Map
      • Parse2 (Input As InputStream) As Map
  • XmlElement
    • Fields:
      • Attributes As Map
      • Children As List
      • ID As String
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • Name As String
      • Text As String
    • Functions:
      • Initialize
        Initializes the fields to their default value.
 

Attachments

  • MashXML Lib 1.01.zip
    10.9 KB · Views: 361
  • MashXML Source Code 1.01.zip
    5.1 KB · Views: 316
Last edited:

Mashiane

Expert
Licensed User
Longtime User
Here is an example of use...

B4X:
Dim import As Map = MashXML.XmlToMap(xmlFile)
    
    'read the project details
    Dim prj As Map = import.Get("project")
    Dim slibraryname As String = prj.GetDefault("libraryname","")
    Dim sauthor As String = prj.GetDefault("author","")
    Dim sversion As String = prj.getdefault("version","")
    'lets update the project details
    ProjectMap.put("projectname",slibraryname)
    ProjectMap.Put("author",sauthor)
    ProjectMap.Put("version",sversion)
    ProjectMap = DBUtils.AddEditRecord(prjDB,"Project","id","1",ProjectMap)
    If etvProject.IsInitialized Then etvProject.SetPropertyBag(ProjectMap)
    
    'define inserts
    Dim einserts As List: einserts.initialize
    Dim minserts As List: minserts.initialize
    Dim pinserts As List: pinserts.initialize
    Dim finserts As List: finserts.initialize
    
    'lets update the classes
    Dim classes As List = import.Get("classes")
    'each class is a map
    For Each classm As Map In classes
        Dim classname As String = classm.Get("classname")
        Dim description As String = classm.Get("description")
        'lets add the class to the database
        Dim dbClass As Map = CreateMap("classname":classname,"description":description,"active":1,"typeof":"Custom View")
        If bClear = True Then
            dbClass = DBUtils.AddRecord(prjDB,"Classes",dbClass)
        Else
            dbClass = DBUtils.AddEditRecord(prjDB,"Classes","ClassName",classname,dbClass)
        End If
        'get the class id
        Dim classid As String = dbClass.Get("id")
        'get the properties
        Dim properties As List = classm.Get("properties")
        For Each property As Map In properties
            Dim prkeyname As String = property.get("keyname")
            Dim prFieldType As String = property.Get("fieldtype")
            Dim prDefaultValue As String = property.get("defaultvalue")
            Dim prdescription As String = property.get("description")
            'process the properties
            Dim pmap As Map = CreateMap("classid":classid,"keyname":prkeyname,"displayname":prkeyname,"fieldtype":prFieldType,"defaultvalue":prDefaultValue,"description":prdescription,"active":"1","list":"")
            If bClear Then
                pinserts.add(pmap)
            Else   
                DBUtils.AddRecord(prjDB,"DesignerProperties",pmap)
            End If
        Next
        'get the fields
        Dim fields As List = classm.Get("fields")
        For Each field As Map In fields
            Dim fldkeyname As String = field.get("keyname")
            Dim fldFieldType As String = field.Get("fieldtype")
            Dim fldDefaultValue As String = field.get("defaultvalue")
            Dim flddescription As String = field.get("description")
            'process the fields
            Dim fmap As Map = CreateMap("classid":classid,"keyname":fldkeyname,"displayname":fldkeyname,"fieldtype":fldFieldType,"defaultvalue":fldDefaultValue,"description":flddescription,"active":"1","list":"")
            If bClear = True Then
                finserts.Add(fmap)
            Else   
                DBUtils.AddRecord(prjDB,"Fields",fmap)
            End If
        Next       
        'get the methods
        Dim methods As List = classm.get("methods")
        For Each method As Map In methods
            Dim mkeyname As String = method.get("keyname")
            Dim mfieldtype As String = method.Get("fieldtype")
            Dim mdescription As String = method.Get("description")
            Dim mdef As String = method.Get("parameterdef")
            Dim mparameters As String = method.Get("parameters")
            'process the methods
            Dim dbMethod As Map = CreateMap("methodname":mkeyname,"active":"1","parameter":mparameters,"parameterdef":mdef,"description":mdescription,"classid":classid,"fieldtype":mfieldtype)
            If bClear = True Then
                minserts.Add(dbMethod)
            Else   
                DBUtils.AddRecord(prjDB,"Methods",dbMethod)
            End If
        Next
        'get the events
        Dim events As List = classm.get("events")
        For Each event As Map In events
            Dim ekeyname As String = event.get("keyname")
            Dim efieldtype As String = event.Get("fieldtype")
            Dim edescription As String = event.Get("description")
            Dim edef As String = event.Get("parameterdef")
            Dim eparameters As String = event.Get("parameters")
            'process the events
            Dim dbEvent As Map = CreateMap("eventname":ekeyname,"active":"1","parameter":eparameters,"parameterdef":edef,"description":edescription,"classid":classid,"fieldtype":efieldtype)
            If bClear = True Then
                einserts.Add(dbEvent)
            Else
                DBUtils.AddRecord(prjDB,"Events",dbEvent)
            End If
        Next
    Next
    If bClear = True Then
        DBUtils.InsertMaps(prjDB,"DesignerProperties",pinserts)
        DBUtils.InsertMaps(prjDB,"Fields",finserts)
        DBUtils.InsertMaps(prjDB,"Methods",minserts)
        DBUtils.InsertMaps(prjDB,"Events",einserts)
    End If
 
Top