B4J Question Need help parsing a complex KML file!

Cableguy

Expert
Licensed User
Longtime User
Hi guys

I have a complex KML (at least to me it is) and I am trying to get a few info out of it....
The file structure (the part I need to parse) looks like this:

B4X:
<Folder>
            <name>Chambre_Merignac_Structure</name>
            <Placemark>
                <styleUrl>#m_ylw-pushpin0</styleUrl>
                <ExtendedData>
                    <SchemaData schemaUrl="#Chambre_Merignac_Structure">
                        <SimpleData name="FID_1">-89451</SimpleData>
                        <SimpleData name="OBJECTID">49179</SimpleData>
                        <SimpleData name="STATUT">E</SimpleData>
                        <SimpleData name="IMPLANT">T</SimpleData>
                        <SimpleData name="NATURE_CHA">R</SimpleData>
                        <SimpleData name="REF_CHAMBR">L5T</SimpleData>
                        <SimpleData name="CODE_COM">33281</SimpleData>
                        <SimpleData name="ID_PROPRIE">1.00000000000</SimpleData>
                        <SimpleData name="ACC_DEPORT">0.00000000000</SimpleData>
                        <SimpleData name="VENTIL">0.00000000000</SimpleData>
                        <SimpleData name="PLAFON">0.00000000000</SimpleData>
                        <SimpleData name="ID_GESTION">0.00000000000</SimpleData>
                        <SimpleData name="INDEX_DOC_">0.00000000000</SimpleData>
                        <SimpleData name="ID_NOEUD">0.00000000000</SimpleData>
                        <SimpleData name="ROTATION">302.50000000000</SimpleData>
                        <SimpleData name="CODE_CH1">9759</SimpleData>
                        <SimpleData name="CODE_CH2">33281</SimpleData>
                        <SimpleData name="NOTE">57DDCE287F26218C1BFE3C5215AB4BC6</SimpleData>
                        <SimpleData name="SECURISEE">0.00000000000</SimpleData>
                        <SimpleData name="CLE_MKT1">33281||</SimpleData>
                        <SimpleData name="CLE_MKT2">9759|33281</SimpleData>
                        <SimpleData name="CLASSE">B</SimpleData>
                        <SimpleData name="X">365789.90000000002</SimpleData>
                        <SimpleData name="Y">1983938.00000000000</SimpleData>
                        <SimpleData name="POINT_X">0.00000000000</SimpleData>
                        <SimpleData name="ID">365789,865500003_1983938,2315</SimpleData>
                        <SimpleData name="Class">Structurant</SimpleData>
                    </SchemaData>
                </ExtendedData>
                <Point>
                    <coordinates>-0.6237452667855941,44.816488002257,0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <styleUrl>#m_ylw-pushpin0</styleUrl>
                <ExtendedData>
                    <SchemaData schemaUrl="#Chambre_Merignac_Structure">
                        <SimpleData name="FID_1">-92385</SimpleData>
                        <SimpleData name="OBJECTID">50767</SimpleData>
                        <SimpleData name="STATUT">E</SimpleData>
                        <SimpleData name="IMPLANT">T</SimpleData>
                        <SimpleData name="NATURE_CHA">H</SimpleData>
                        <SimpleData name="REF_CHAMBR">OHN</SimpleData>
                        <SimpleData name="REF_NOTE">F2.</SimpleData>
                        <SimpleData name="CODE_COM">33318</SimpleData>
                        <SimpleData name="CODE_VOIE">3280</SimpleData>
                        <SimpleData name="NUM_VOIE">52</SimpleData>
                        <SimpleData name="ID_PROPRIE">1.00000000000</SimpleData>
                        <SimpleData name="ACC_DEPORT">0.00000000000</SimpleData>
                        <SimpleData name="VENTIL">0.00000000000</SimpleData>
                        <SimpleData name="PLAFON">0.00000000000</SimpleData>
                        <SimpleData name="ID_GESTION">0.00000000000</SimpleData>
                        <SimpleData name="QUANTIFICA">#PA:6#</SimpleData>
                        <SimpleData name="INDEX_DOC_">0.00000000000</SimpleData>
                        <SimpleData name="ID_NOEUD">0.00000000000</SimpleData>
                        <SimpleData name="ROTATION">284.00000000000</SimpleData>
                        <SimpleData name="CODE_CH1">6079</SimpleData>
                        <SimpleData name="CODE_CH2">33318</SimpleData>
                        <SimpleData name="NOTE">57DDCE290BD070D81BFE3C4BA6AAC648</SimpleData>
                        <SimpleData name="SECURISEE">0.00000000000</SimpleData>
                        <SimpleData name="CLE_MKT1">33318|3280|52</SimpleData>
                        <SimpleData name="CLE_MKT2">6079|33318</SimpleData>
                        <SimpleData name="CODE_CH1_C">A717</SimpleData>
                        <SimpleData name="CODE_CH2_P">33318</SimpleData>
                        <SimpleData name="CLASSE">B</SimpleData>
                        <SimpleData name="X">365871.00000000000</SimpleData>
                        <SimpleData name="Y">1983760.00000000000</SimpleData>
                        <SimpleData name="POINT_X">0.00000000000</SimpleData>
                        <SimpleData name="ID">365870,976000004_1983760,371</SimpleData>
                        <SimpleData name="Class">Structurant</SimpleData>
                    </SchemaData>
                </ExtendedData>
                <Point>
                    <coordinates>-0.622636421284825,44.8149168698937,0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <styleUrl>#m_ylw-pushpin0</styleUrl>
                <ExtendedData>
                    <SchemaData schemaUrl="#Chambre_Merignac_Structure">
                        <SimpleData name="FID_1">-95887</SimpleData>
                        <SimpleData name="OBJECTID">52707</SimpleData>
                        <SimpleData name="STATUT">E</SimpleData>
                        <SimpleData name="IMPLANT">T</SimpleData>
                        <SimpleData name="NATURE_CHA">H</SimpleData>
                        <SimpleData name="REF_CHAMBR">OHN</SimpleData>
                        <SimpleData name="REF_NOTE">B2.</SimpleData>
                        <SimpleData name="CODE_COM">33318</SimpleData>
                        <SimpleData name="CODE_VOIE">3280</SimpleData>
                        <SimpleData name="NUM_VOIE">67</SimpleData>
                        <SimpleData name="ID_PROPRIE">1.00000000000</SimpleData>
                        <SimpleData name="ACC_DEPORT">0.00000000000</SimpleData>
                        <SimpleData name="VENTIL">0.00000000000</SimpleData>
                        <SimpleData name="PLAFON">0.00000000000</SimpleData>
                        <SimpleData name="ID_GESTION">0.00000000000</SimpleData>
                        <SimpleData name="QUANTIFICA">#PA:36#</SimpleData>
                        <SimpleData name="INDEX_DOC_">0.00000000000</SimpleData>
                        <SimpleData name="ID_NOEUD">0.00000000000</SimpleData>
                        <SimpleData name="ROTATION">286.00000000000</SimpleData>
                        <SimpleData name="CODE_CH1">6084</SimpleData>
                        <SimpleData name="CODE_CH2">33318</SimpleData>
                        <SimpleData name="NOTE">57DDCE2922E678FB1BFE3C4BCFAACBE1</SimpleData>
                        <SimpleData name="SECURISEE">0.00000000000</SimpleData>
                        <SimpleData name="CLE_MKT1">33318|3280|67</SimpleData>
                        <SimpleData name="CLE_MKT2">6084|33318</SimpleData>
                        <SimpleData name="CODE_CH1_C">A1782</SimpleData>
                        <SimpleData name="CODE_CH2_P">33318</SimpleData>
                        <SimpleData name="CLASSE">B</SimpleData>
                        <SimpleData name="X">365836.20000000001</SimpleData>
                        <SimpleData name="Y">1983867.00000000000</SimpleData>
                        <SimpleData name="POINT_X">0.00000000000</SimpleData>
                        <SimpleData name="ID">365836,141000003_1983867,3125</SimpleData>
                        <SimpleData name="Class">Structurant</SimpleData>
                    </SchemaData>
                </ExtendedData>
                <Point>
                    <coordinates>-0.6231271746075751,44.8158662591759,0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <styleUrl>#m_ylw-pushpin0</styleUrl>
                <ExtendedData>
                    <SchemaData schemaUrl="#Chambre_Merignac_Structure">
                        <SimpleData name="FID_1">-96097</SimpleData>
                        <SimpleData name="OBJECTID">52822</SimpleData>
                        <SimpleData name="STATUT">E</SimpleData>
                        <SimpleData name="IMPLANT">C</SimpleData>
                        <SimpleData name="NATURE_CHA">H</SimpleData>
                        <SimpleData name="REF_CHAMBR">OHN</SimpleData>
                        <SimpleData name="REF_NOTE">1TER PRO</SimpleData>
                        <SimpleData name="CODE_COM">33281</SimpleData>
                        <SimpleData name="CODE_VOIE">2780</SimpleData>
                        <SimpleData name="NUM_VOIE">83</SimpleData>
                        <SimpleData name="ID_PROPRIE">1.00000000000</SimpleData>
                        <SimpleData name="ACC_DEPORT">0.00000000000</SimpleData>
                        <SimpleData name="VENTIL">0.00000000000</SimpleData>
                        <SimpleData name="PLAFON">0.00000000000</SimpleData>
                        <SimpleData name="ID_GESTION">0.00000000000</SimpleData>
                        <SimpleData name="INDEX_DOC_">0.00000000000</SimpleData>
                        <SimpleData name="ID_NOEUD">0.00000000000</SimpleData>
                        <SimpleData name="ROTATION">301.30000000000</SimpleData>
                        <SimpleData name="CODE_CH1">7365</SimpleData>
                        <SimpleData name="CODE_CH2">33281</SimpleData>
                        <SimpleData name="NOTE">57DDCE28D1E669181BFE3C4631AA35C7</SimpleData>
                        <SimpleData name="SECURISEE">0.00000000000</SimpleData>
                        <SimpleData name="CLE_MKT1">33281|2780|83</SimpleData>
                        <SimpleData name="CLE_MKT2">7365|33281</SimpleData>
                        <SimpleData name="CODE_CH1_C">810</SimpleData>
                        <SimpleData name="CODE_CH2_P">33281</SimpleData>
                        <SimpleData name="CLASSE">B</SimpleData>
                        <SimpleData name="X">365746.09999999998</SimpleData>
                        <SimpleData name="Y">1984005.00000000000</SimpleData>
                        <SimpleData name="POINT_X">0.00000000000</SimpleData>
                        <SimpleData name="ID">365746,077_1984005,107</SimpleData>
                        <SimpleData name="Class">Structurant</SimpleData>
                    </SchemaData>
                </ExtendedData>

This "SchemaData" is repeated about 200 times, and from each block I need to extract the REF_CHAMBR and the CODE_CH1 values, but I just can't retrieve those values by name, nor I get to log the complete SimpleData as string so I could work them out of it.

This is my first try/use of xml parsing, so please be gentle with me!

I just need some pointers, like, should I try to get them within startelement or endelement?
How should I logic out that structure?
 

Cableguy

Expert
Licensed User
Longtime User
Thanks Don, Got me on the Right direction, Only one last question, I used the conversion to Json from the example, but I cannot differentiate from what is a MAP or a List in the JSON string, are they differentiated by the [ and the {, if so, which is which?
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
a small example in B4J

B4X:
Sub klmParse
    'https://www.b4x.com/android/forum/threads/need-help-parsing-a-complex-kml-file.88076/
    Dim xm As Xml2Map
    xm.Initialize
    ParsedData = xm.Parse( File.ReadString( File.DirAssets, "klm.txt"))
    Dim Folder As Map = ParsedData.Get("Folder")
    Dim Placemarks As List = Folder.Get("Placemark")
    For Each Placemark As Map In Placemarks
        'Log( Placemark)
        Dim ExtendedData As Map = Placemark.Get("ExtendedData")
        'Log( ExtendedData)
       
        Dim SchemaData As Map = ExtendedData.Get("SchemaData")
        'Log( SchemaData)
       
        Dim SimpleData As List = SchemaData.Get("SimpleData")
        'Log( SimpleData)
       
        For Each Attributes As Map In SimpleData
            'Log( Attributes)
           
            Dim Attribute As Map = Attributes.Get( "Attributes")
            'Log( Attribute)
           
            Dim Attrib_Name As String = Attribute.Get( "name")
            'Log( Attrib_Name)
       
            Dim Attrib_Text As String = Attributes.Get( "Text")
            'Log( Attrib_Text)
           
            If Attrib_Name = "REF_CHAMBR" Or Attrib_Name = "CODE_CH1" Then
                Log( Attrib_Name &TAB& Attrib_Text)
            End If
        Next
       
    Next
End Sub

Results:
REF_CHAMBR L5T
CODE_CH1 9759
REF_CHAMBR OHN
CODE_CH1 6079
REF_CHAMBR OHN
CODE_CH1 6084
REF_CHAMBR OHN
CODE_CH1 7365

EDIT:

Map = {
List = [
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Awsome, Thank You
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you just need a couple of values from the file, then maybe you could use a simpler method.
1, Read the file into a list using textreader
2, for each the lines and look for the specific entries you want
3, get the substring for the data
ie
B4X:
 Dim trIn As TextReader
 trIn.Initialize(File.OpenInput("c:/temp","problem xml.xml")) ' your file here
 Dim l As List = trIn.ReadList
 trIn.Close
 For Each line As String In l
  If line.Contains($""REF_CHAMBR""$) Then
    Log("REF_CHAMBR >> "&line.SubString2(line.IndexOf(">")+1,line.LastIndexOf("<")))
   End If
   If line.Contains($""CODE_CH1""$) Then
    Log("CODE_CH1 >> "&line.SubString2(line.IndexOf(">")+1,line.LastIndexOf("<")))
  End If
 Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…