Browse and replace based on the <name> property using php

Asked

Viewed 44 times

1

I’m a beginner in php and I’m having trouble realizing the following logic:

I have this code in xml (part of the code) and need to change the property Icon based on occurrences in name. For example if the name for 529383 as in the code below I modify ylw-pushpin for 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>

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 upload the file, scroll through the document nodes and modify the desired values. In this example I created an array to help change the Icon 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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.