4

I can get all the data's except lat,lng and address.

How to get these data's , where i am going wrong?

Here is my code

<device_lastupdate>
<version>1.0</version>
<device>
    <id>012980000234020</id>
    <updated_at type="dateTime">2015-01-28T10:21:50+05:30</updated_at>
    <location>
        <lat type="float">12.828383</lat>
        <lng type="float">79.695983</lng>
        <address>Karukinil Amarthanval Street</address>
    </location>
    <odometer type="float">12341</odometer>
    <speed type="float">0</speed>
    <ignition>false</ignition>
</device>

and here is my php code

$xml = simplexml_load_file("data.xml");
echo "<h2>".$xml->getName()."</h2><br />";

foreach($xml->children() as $device)
{
    echo "ID : ".$device->id."<br />";
    echo "Updated At : ".$device->updated_at." <br />";
    echo "Latitude : ".$device->lat." <br />";
    echo "Longitude : ".$device->lng." <br />";
    echo "Address : ".$device->address." <br />";

    echo "Odometer : ".$device->odometer." <br />";
    echo "Speed: ".$device->speed." <br />";
    echo "Ignition:".$device->ignition."<br />";
    echo "<hr/>";
}
5
  • I don't suppose you could break that XML one-liner into a more readable form? Commented Jan 29, 2015 at 5:13
  • I am able to get data for lat, lang and address data , what else you want here. The above mention is correct. Commented Jan 29, 2015 at 5:21
  • @zan ,these 3 are my problem , how to get those data anyway?can you please post your code? Commented Jan 29, 2015 at 5:35
  • @zan , sorry previously i had missed one tag in XML Code , please check now once again and help to solve :-) Commented Jan 29, 2015 at 5:41
  • You have added only one node in xml. Can you add your parent node & multiple child in given xml. Ex:.<parent><child></child><child></child></parent>. Commented Jan 29, 2015 at 6:07

1 Answer 1

4

We can make use of this small function that takes object and attribute and get its value.

<?php
        $xml = simplexml_load_file("data.xml");
        echo "<h2>".$xml->getName()."</h2><br />";

        function xml_attribute($object, $attribute)
        {
            if(isset($object[$attribute]))
                return (string) $object[$attribute];
        }

        foreach($xml->children() as $device)
        {
            var_dump($device->location);
            $lng =  xml_attribute($device->location->lng, 0);
            $lat =  xml_attribute($device->location->lat, 0);
            $address =  xml_attribute($device->location->address, 0);
            echo "BOOK : ".$device->id."<br />";
            echo "Author : ".$device->updated_at." <br />";
            echo "Title : ".$lng." <br />";
            echo "Genre : ".$lat." <br />";
            echo "Price : ".$address." <br />";

            echo "Publish Date : ".$device->odometer." <br />";
            echo "Description : ".$device->speed." <br />";
            echo "Ignition:".$device->ignition."<br />";
            echo "<hr/>";
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Mr.Zan :-) , you are a genius :-) , it's absolutely fine now :-) Many thanks :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.