B4J Question xmlparsing with xml2map

Discussion in 'B4J Questions' started by Pedro Caldeira, Mar 23, 2018.

Tags:
  1. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Hello,
    I am trying to parse a xml file with the xm2map module

    i get a map not initialized in the example code

    Code:
    Sub GetElements (m As Map, key As StringAs List
        
    Dim res As List
        
    If m.ContainsKey(key) = False Then <- line37
            res.Initialize
            
    Return res
        
    Else
            
    Dim value As Object = m.Get(key)
            
    If value Is List Then Return value
            res.Initialize
            res.Add(value)
            
    Return res
        
    End If
    End Sub
    Error:

    Waiting for debugger to connect...
    Program started.
    Error occurred on line: 37 (Main)
    java.lang.RuntimeException: Object should first be initialized (Map).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
    at anywheresoftware.b4a.objects.collections.Map.ContainsKey(Map.java:122)
    at b4j.example.main._getelements(main.java:139)
    at b4j.example.main._gomain(main.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.main(main.java:29)
     
  2. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    The parameter m is not initialized, most likely your parsing went wrong or you are passing the wrong map to that function.
     
  3. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    i am using
    Code:
    Dim rowset As Map = ParsedData.Get("rowset")
    For Each row As Map In GetElements(rowset, "row")
          
    Dim description As String = row.Get("description")
          
    Log(description)
    Next
    file:
    the row element repeats several times

    Code:
    <table>
     <rowset>
        <row>
          <
    id>1794540415488854</id>
          <family_id>
    1794952732344400</family_id>
          <model_id>
    0</model_id>
          <code></code>
          <order_code>
    0</order_code>
          <description>So Gambas</description>
          <shortname>So Gambas</shortname>
          <button_name>So Gambas</button_name>
          <button_color>-
    1</button_color>
          <button_order>
    17</button_order>
          <productdiscount_id>
    0</productdiscount_id>
          <productcommission_id>
    0</productcommission_id>
          <print_sort_order>
    0</print_sort_order>
          <print_sort_doc>
    0</print_sort_doc>
          <printzone_id>
    1794952732344338</printzone_id>
          <taxgroup_id>
    1003002</taxgroup_id>
          <change_sale_price>
    0</change_sale_price>
          <weight>
    0</weight>
          <base_unit_id>
    11001</base_unit_id>
          <forsale>
    1</forsale>
          <forpurchase>
    0</forpurchase>
          <forproduction>
    0</forproduction>
          <generic_id>
    0</generic_id>
          <obs></obs>
          <stockconfig_id>
    7501</stockconfig_id>
          <setexpireday>
    0</setexpireday>
          <saleprice>
    0</saleprice>
          <purchaseprice>
    0</purchaseprice>
          <image_id>
    0</image_id>
          <default_sale_unit_id>
    11001</default_sale_unit_id>
          <default_purchase_unit_id>
    11001</default_purchase_unit_id>
          <default_stock_unit_id>
    11001</default_stock_unit_id>
          <label_unit_id>
    11001</label_unit_id>
          <markup>
    0</markup>
          <status>
    1</status>
          <product_type>
    3</product_type>
          <modelgridtype_id>
    0</modelgridtype_id>
          <default_supplier_id>
    0</default_supplier_id>
          <fixedsupplier>
    0</fixedsupplier>
          <creation_date>
    1485118508079399538</creation_date>
          <productcategory_id>
    0</productcategory_id>
          <deleted>
    0</deleted>
        </row>
     </rowset>
    </table>
     
  4. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    let me test it, but my guess is that rowset is not in the map you are looking for, probably one map below.
     
  5. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    this code doesnt throw any error

    Code:
    Dim s As String = $"
            <table>
            <rowset>
                <row>
                  <id>1794540415488854</id>
                  <family_id>1794952732344400</family_id>
                  <model_id>0</model_id>
                  <code></code>
                  <order_code>0</order_code>
                  <description>So Gambas</description>
                  <shortname>So Gambas</shortname>
                  <button_name>So Gambas</button_name>
                  <button_color>-1</button_color>
                  <button_order>17</button_order>
                  <productdiscount_id>0</productdiscount_id>
                  <productcommission_id>0</productcommission_id>
                  <print_sort_order>0</print_sort_order>
                  <print_sort_doc>0</print_sort_doc>
                  <printzone_id>1794952732344338</printzone_id>
                  <taxgroup_id>1003002</taxgroup_id>
                  <change_sale_price>0</change_sale_price>
                  <weight>0</weight>
                  <base_unit_id>11001</base_unit_id>
                  <forsale>1</forsale>
                  <forpurchase>0</forpurchase>
                  <forproduction>0</forproduction>
                  <generic_id>0</generic_id>
                  <obs></obs>
                  <stockconfig_id>7501</stockconfig_id>
                  <setexpireday>0</setexpireday>
                  <saleprice>0</saleprice>
                  <purchaseprice>0</purchaseprice>
                  <image_id>0</image_id>
                  <default_sale_unit_id>11001</default_sale_unit_id>
                  <default_purchase_unit_id>11001</default_purchase_unit_id>
                  <default_stock_unit_id>11001</default_stock_unit_id>
                  <label_unit_id>11001</label_unit_id>
                  <markup>0</markup>
                  <status>1</status>
                  <product_type>3</product_type>
                  <modelgridtype_id>0</modelgridtype_id>
                  <default_supplier_id>0</default_supplier_id>
                  <fixedsupplier>0</fixedsupplier>
                  <creation_date>1485118508079399538</creation_date>
                  <productcategory_id>0</productcategory_id>
                  <deleted>0</deleted>
                </row>
            </rowset>
            </table>   
        "$
       
           
        
    Dim xm As Xml2Map
        xm.Initialize
        
    Dim parseMap0 As Map = xm.Parse(s)
        
    Dim table As Map = parseMap0.Get("table")
        
    Dim rowSet As Map = table.Get("rowset")
       
        
    For Each row As Map In GetElements(rowSet, "row")
            
    Dim description As String = row.Get("description")
            
    Log(description)
        
    Next
     
    Erel and Pedro Caldeira like this.
  6. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Worked.
    Thanks
     
    Last edited: Mar 24, 2018
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice