B4J Question [SOLVED]XML Edit (convert to Map, edit then Back again or Edit XML directly)

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All, I am faced with a few choices , maybe more if you have one. I have a X<L file which has multiple lines of COLOR information, there can be any amount of lines from 1 to 30. I could take all the lines before and store it into a list and all the lines afterwards and store it into another list , generate the lines using code into a third list then when I write the xml file process the first list , then the third list and lastly the second list . OR write the changes into maps and convert the maps back into XML. Just extracting the areas for this was a long process.

Any suggestions ?

1720473576480.png

This is the XML code

This is the code i had to do to get to the lines of data required

B4X:
Sub Button1_Click
    Dim map As Map
        map.Initialize

    Dim xm As Xml2Map
        xm.Initialize
  
    map = xm.Parse(File.ReadString(File.DirAssets, "data.xml"))
  
    Dim attr As Map = map.Get("mad_storage_place")
    Dim bttr As Map =  attr.Get("place")
    Dim cttr As Map = bttr.Get("effect")
    Dim dttr As Map = cttr.Get("facade")
    Dim ettr As Map = dttr.Get("renderer")
    Dim fttr As Map = ettr.Get("effect_feature_color_list")
    Dim gttr As Map = fttr.Get("color_list")
    Dim httr As List = gttr.Get("color")
    For x = 0 To httr.Size-1
        Log("=============== "& x &" ================")
      
        Dim final As Map = httr.Get(x)
        Dim valu As Map = final.Get("Attributes")
  
            For i = 0 To valu.Size - 1
                Log("Key: " & valu.GetKeyAt(i))
                Log("Value: " & valu.GetValueAt(i))
            Next
        Log("=======================================")
    Next
  
End Sub

This was the result

1720473692776.png


B4X:
<?xml version="1.0" encoding="UTF-8"?>
<mad_storage_place version="0x03000000" product_version="0x03060901">
    <product>DF</product>
    <save_date>2024/07/08 19:16:48</save_date>
    <place version="0x03000000" product_version="0x03060901">
        <thumb version="0x03000000">
            <type value="0"/>
            <width value="100"/>
            <height value="100"/>
            <width_bytes value="400"/>
            <planes value="1"/>
            <bits_pixel value="32"/>
            <data_raw_comp value_comp_base64="AACcQHic7dHBCcNADADBK+Yqcp+u0fkcTvwLITb7GMT8BALtnHPMm41xnLblOvvysbktx9u+/LL4j7t3/+kperTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aJHix4terTo0aLH917rEm5A"/>
        </thumb>
        <macro_storage_place version="0x03000000" info="macro stopped" product_version="0x03060901">
            <script_wrapper version="0x03000000">
                <script_engine value="NO_ENGINE"/>
            </script_wrapper>
        </macro_storage_place>
        <effect version="0x03000000" info="SCE Color Scroll" product_version="0x03060901" effect_id="934F5C38-3403-4A33-8B0E-35A4F8CCC68E" type="0">
            <facade version="0x03000000">
                <renderer version="0x03000000">
                    <effect_feature_color_list version="0x03000000" index="0">
                        <color_list version="0x03000000" info="6 entries">
                            <color version="0x03000000" r="255" g="0" b="0" w="0" a="0" pos="0" fade="1"/>
                            <color version="0x03000000" r="255" g="255" b="0" w="0" a="0" pos="0.2" fade="1"/>
                            <color version="0x03000000" r="0" g="255" b="0" w="0" a="0" pos="0.4" fade="1"/>
                            <color version="0x03000000" r="0" g="255" b="255" w="0" a="0" pos="0.6" fade="1"/>
                            <color version="0x03000000" r="0" g="0" b="255" w="0" a="0" pos="0.8" fade="1"/>
                            <color version="0x03000000" r="255" g="0" b="255" w="0" a="0" pos="1" fade="1"/>
                        </color_list>
                        <color_list_fade value="0"/>
                    </effect_feature_color_list>
                    <chaser version="0x03040000" info="Effect Parameter Chaser"/>
                    <macro_effect version="0x03000000" info="macro stopped" product_version="0x03060901">
                        <script_wrapper version="0x03000000">
                            <script_engine value="NO_ENGINE"/>
                        </script_wrapper>
                    </macro_effect>
                </renderer>
            </facade>
        </effect>
    </place>
</mad_storage_place>
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Would probably make it easier for people to try if you posted the xml as text, else they would need to type it all in.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
?
Xml2Map
Map2Xml
XMLBuilder

The Library is Xml2Map. It is a b4xlib
Inside it there is a Class Map2Xml which uses - internally - XmlBuiler.

Map2Xml - New class!

Map2Xml converts the map created with Xml2Map to a Xml string. It uses XmlBuilder library and it is compatible with B4A, B4i and B4J.
It can be used to modify existing XML documents. You read the document with Xml2Map, make the changes in the returned map and write it back with Map2Xml.
 
Last edited:
Upvote 0
Top