如何使用PHP提取XML数据数据?
How to Extract XML Data data using PHP?
我可以获取除纬度、经度和地址之外的所有数据。
如何获取这些数据,我哪里出错了?
这是我的代码
<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>
这是我的 php 代码
$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/>";
}
我们可以利用这个获取对象和属性并获取其值的小函数。
<?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/>";
}
我可以获取除纬度、经度和地址之外的所有数据。
如何获取这些数据,我哪里出错了?
这是我的代码
<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>
这是我的 php 代码
$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/>";
}
我们可以利用这个获取对象和属性并获取其值的小函数。
<?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/>";
}