Get xml data with php [closed]

0

I've never worked with XML. Below I put the XML as I get from a URL. I would like to get the latitude and longitude data and pass it to a PHP variable.

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <feedMessageResponse>
        <count>10</count>
        <feed>
            <id>03XHH0sPyTiYUsYD2TVJ4q7CzEH89HBhG</id>
            <name>OneAtATime</name>
            <description>OneAtATime</description>
            <status>ACTIVE</status>
            <usage>0</usage>
            <daysRange>7</daysRange>
            <detailedMessageShown>true</detailedMessageShown>
        </feed>
        <totalCount>10</totalCount>
        <activityCount>0</activityCount>
        <messages>
            <message clientUnixTime="0">
                <id>4937065</id>
                <messengerId>0-8356068</messengerId>
                <messengerName>Spot2</messengerName>
                <unixTime>1364909292</unixTime>
                <messageType>HELP-CANCEL</messageType>
                <latitude>-99999.0</latitude>
                <longitude>-99999.0</longitude>
                <modelId>SPOT2</modelId>
                <showCustomMsg>Y</showCustomMsg>
                <dateTime>2013-04-02T06:28:12-0700</dateTime>
                <hidden>0</hidden>
                <messageContent>The help message has been cancelled</messageContent>
            </message>
            <message clientUnixTime="0">
                <id>4937064</id>
                <messengerId>0-8356068</messengerId>
                <messengerName>Spot2</messengerName>
                <unixTime>1364909283</unixTime>
                <messageType>HELP</messageType>
                <latitude>45.42249</latitude>
                <longitude>-111.68832</longitude>
                <modelId>SPOT2</modelId>
                <showCustomMsg>Y</showCustomMsg>
                <dateTime>2013-04-02T06:28:03-0700</dateTime>
                <hidden>0</hidden>
                <messageContent>This is the default HELP message. Please update.</messageContent>
            </message>
            <message clientUnixTime="0">
                <id>4937060</id>
                <messengerId>0-8356068</messengerId>
                <messengerName>Spot2</messengerName>
                <unixTime>1364908774</unixTime>
                <messageType>CUSTOM</messageType>
                <latitude>45.42249</latitude>
                <longitude>-111.68832</longitude>
                <modelId>SPOT2</modelId>
                <showCustomMsg>Y</showCustomMsg>
                <dateTime>2013-04-02T06:19:34-0700</dateTime>
                <hidden>0</hidden>
                <messageContent>This is a custom message</messageContent>
            </message>
            <message clientUnixTime="0">
                <id>4937059</id>
                <messengerId>0-8356068</messengerId>
                <messengerName>Spot2</messengerName>
                <unixTime>1364908765</unixTime>
                <messageType>OK</messageType>
                <latitude>45.42249</latitude>
                <longitude>-111.68832</longitude>
                <modelId>SPOT2</modelId>
                <showCustomMsg>Y</showCustomMsg>
                <dateTime>2013-04-02T06:19:25-0700</dateTime>
                <hidden>0</hidden>
                <messageContent>This is the default SPOT Check-in/OK message. Please update.</messageContent>
            </message>
            <message clientUnixTime="0">
                <id>4937057</id>
                <messengerId>0-8356068</messengerId>
                <messengerName>Spot2</messengerName>
                <unixTime>1364908512</unixTime>
                <messageType>TRACK</messageType>
                <latitude>45.42249</latitude>
                <longitude>-111.68832</longitude>
                <modelId>SPOT2</modelId>
                <showCustomMsg>Y</showCustomMsg>
                <dateTime>2013-04-02T06:15:12-0700</dateTime>
                <hidden>0</hidden>
            </message>
        </messages>
    </feedMessageResponse>
</response>
    
asked by anonymous 10.08.2017 / 03:27

2 answers

1

The format of the answer is kind of weird, so I had to loop to show all latitudes and longitudes.

Try to do this:

$xml = simplexml_load_file('url-do-xml');

$i = 1;
foreach($xml->feedMessageResponse->messages->message as $message) {
    print_r("Latitude $i: $message->latitude\n");
    print_r("Longitude $i: $message->longitude\n\n");
    $i++;
}

Replace url-do-xml with the url you get xml.

If you can not open the xml directly from the link you can create a temporary xml file with this response and open it in simplexml_load_file .

Result:

Latitude 1: -99999.0
Longitude 1: -99999.0

Latitude 2: 45.42249
Longitude 2: -111.68832

Latitude 3: 45.42249
Longitude 3: -111.68832

Latitude 4: 45.42249
Longitude 4: -111.68832

Latitude 5: 45.42249
Longitude 5: -111.68832
    
10.08.2017 / 14:14
1

To read the xml file:

$arquivo="xml.xml";//arquivo
$xml = simplexml_load_file($arquivo);/* Lê o arquivo XML e recebe um objeto com as informações */
$total = count($xml->feedMessageResponse->messages->message);//número de elementos

//usado com bloco array
$localizacao = array();

/* Percorre o objeto e imprime as informações na tela */
for ($i = 0; $i < $total; $i++):

   //uma variavel com todos os elementos
   $variavel = $variavel. ($xml->feedMessageResponse->messages->message[$i]->latitude)." ".($xml->feedMessageResponse->messages->message[$i]->longitude)." ";

   //imprime linha a linha
   echo "Latitude: ";
   print($xml->feedMessageResponse->messages->message[$i]->latitude);
   echo " ::: Longitude: ";
   print($xml->feedMessageResponse->messages->message[$i]->longitude);
   echo "<br>";

   //bloco array
   $result = ($xml->feedMessageResponse->messages->message[$i]->latitude)." ".($xml->feedMessageResponse->messages->message[$i]->longitude);       
   //array_push() recebe dois argumentos: um array, e um elemento para adicionar no final deste array      
   array_push($localizacao, $result);
   //fim bloco array

endfor;

echo "<br>Variavel: ";
echo "<br>";
echo $variavel;
echo "<br>";
echo "<br> Array";
echo "<br>";
print_r($localizacao);
  

By using dynamic variables, each variable ($ s0, $ s1, $ s2 ...) with a latitude and a longitude

for ($i = 0; $i < $total; $i++){    
   $n = "s".$i;
   $$n= ($xml->feedMessageResponse->messages->message[$i]->latitude)." ".($xml->feedMessageResponse->messages->message[$i]->longitude);
   echo $$n;
   echo "<br>";
}
  • SimpleXml is a php library that makes it easy to read xml files without the need to write many codes.
10.08.2017 / 14:31