Browse and replace based on name property using php

1

I'm new to php and I'm having a hard time performing the following logic:

I have this code in xml (part of the code) and I need to change the Icon property based on occurrences in name . For example if name is 529383 as in the code below I modify ylw-pushpin to red-pushpin in icon .

<name>529383</name>
            <visibility>0</visibility>
            <Snippet maxLines="0" id="s30"></Snippet>
            <LookAt>
                <longitude>-46.545475</longitude>
                <latitude>-23.456651</latitude>
                <altitude>0</altitude>
                <heading>0</heading>
                <tilt>0</tilt>
                <range>1000</range>
                <altitudeMode>relativeToGround</altitudeMode>
            </LookAt>
            <styleUrl>#myicon_3028</styleUrl>
            <Style id="sh_30">
                <IconStyle>
                    <scale>1.3</scale>
                    <Icon>
                        <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                    </Icon>
                </IconStyle>
            </Style>
            <Point>
                <coordinates>-46.545475,-23.456651,0</coordinates>
            </Point>
        </Placemark>
    
asked by anonymous 27.10.2017 / 14:39

1 answer

0

I suppose your xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<places>
    <Placemark>  
        <name>529383</name>
        <visibility>0</visibility>
        <Snippet maxLines="0" id="s30"></Snippet>
        <LookAt>
            <longitude>-46.545475</longitude>
            <latitude>-23.456651</latitude>
            <altitude>0</altitude>
            <heading>0</heading>
            <tilt>0</tilt>
            <range>1000</range>
            <altitudeMode>relativeToGround</altitudeMode>
        </LookAt>
        <styleUrl>#myicon_3028</styleUrl>
        <Style id="sh_30">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>
                    http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
                    </href>
                </Icon>
            </IconStyle>
        </Style>
        <Point>
            <coordinates>-46.545475,-23.456651,0</coordinates>
        </Point>
    </Placemark>

    <Placemark>  
        <name>123456</name>
        <visibility>0</visibility>
        <Snippet maxLines="0" id="s30"></Snippet>
        <LookAt>
            <longitude>-46.545475</longitude>
            <latitude>-23.456651</latitude>
            <altitude>0</altitude>
            <heading>0</heading>
            <tilt>0</tilt>
            <range>1000</range>
            <altitudeMode>relativeToGround</altitudeMode>
        </LookAt>
        <styleUrl>#myicon_3028</styleUrl>
        <Style id="sh_30">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>
                    http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
                    </href>
                </Icon>
            </IconStyle>
        </Style>
        <Point>
            <coordinates>-46.545475,-23.456651,0</coordinates>
        </Point>
    </Placemark>

    <Placemark>  
        <name>124816</name>
        <visibility>0</visibility>
        <Snippet maxLines="0" id="s30"></Snippet>
        <LookAt>
            <longitude>-46.545475</longitude>
            <latitude>-23.456651</latitude>
            <altitude>0</altitude>
            <heading>0</heading>
            <tilt>0</tilt>
            <range>1000</range>
            <altitudeMode>relativeToGround</altitudeMode>
        </LookAt>
        <styleUrl>#myicon_3028</styleUrl>
        <Style id="sh_30">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>
                    http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png
                    </href>
                </Icon>
            </IconStyle>
        </Style>
        <Point>
            <coordinates>-46.545475,-23.456651,0</coordinates>
        </Point>
    </Placemark>
</places>

Then just load the file, scroll through the document nodes and change the values you want. In this example I created an array to assist in changing the icons according to the name. Then:

<?php
$xml = simplexml_load_file('xml.xml');

var_dump($xml);

$associacoes = ['529383' => 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin1.png',
'123456' => 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin2.png',
'124816' => 'http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin3.png'];

echo 'antes:<br>';
foreach($xml->children() as $placemark){
    /*********************************************
    Exibição
    *********************************************/
    echo $placemark->name;
    echo '<br>';
    echo $placemark->Style->IconStyle->Icon->href;
    echo '<br><br>';

    /*********************************************
    Modificação
    *********************************************/
    if(isset($associacoes[(string)$placemark->name])){
        $placemark->Style->IconStyle->Icon->href = $associacoes[(string)$placemark->name];
    }

}

echo 'Depois:<br>';

foreach($xml->children() as $placemark){
    /*********************************************
    Exibição
    *********************************************/
    echo $placemark->name;
    echo '<br>';
    echo $placemark->Style->IconStyle->Icon->href;
    echo '<br><br>';
}
    
27.10.2017 / 20:58