Retrieve xml data with php

Asked

Viewed 1,364 times

0

I never worked with XML. Below I put the XML code as I get from a URL. I would like to take the latitude and longitude data and move on 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>
  • 1

    You can start by reading the documentation: Manipulation of XML.

  • Poxa my director I still can not understand if you give me to advance I thank much

  • Try these then: https://answall.com/search?q=%5Bphp%5D+xml

  • Nothing yet blz leaves still thank you there by the force

2 answers

1


The answer format is a little strange, so I had to make a loop to show all the latitudes and longitudes.

Try to do it this way:

$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++;
}

Substitute url-do-xml by the url you get the xml.

If you cannot open the xml directly by the link you can create a temporary xml file with this answer and open it in the function simplexml_load_file.

Upshot:

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
  • Buddy how I could catch just from latitude 1

  • @Hemersonprestes $latitude = $xml->feedMessageResponse->messages->message[0]->latitude;

  • Top my friend worked out thanks !

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);

Making use of 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, which facilitates reading of xml files without the need to write many codes.

Browser other questions tagged

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