解析xml中缺少的属性

时间:2012-03-10 08:50:08

标签: php xml

我有一个xml输出,其中包含某些子元素的缺少属性。我如何解析并将它们插入我的数据库? upper元素具有属性“latitude”和“longitude”,而第二个元素不包含这些属性 请参阅下面的xml结构摘录:

<response .....
<listings bathroom_number="1" bedroom_number="0" datasource_name="FindaProperty.com" guid="g1-jNtMTMxADMzIjM=E" img_height="120" img_url="http://2.l.uk.nestoria.nestimg.com/1v0/2/1/1v021c995ea319948304290aa563f0478ddf67b99e.2.jpg" img_width="160" keywords="Furnished" latitude="51.54570" lister_name="Gowerlane" listing_type="let" location_accuracy="9" longitude="-0.20220" price="180" price_coldrent="0" price_currency="GBP" price_formatted="180 GBP per week" price_high="180" price_low="180" price_type="weekly" property_type="flat" summary="We are the professional landlords and not agents. with immediate access to..."  title="Kilburn High Road, Kilburn, NW6" />
<listings bathroom_number="" bedroom_number="0" datasource_name="PropertyIndex" guid="g1-TNtMDN5YDO3EQO==" img_height="120" img_url="http://1.l.uk.nestoria.nestimg.com/1vb/0/4/1vb04a1d2f88c68d0b1b3e44c32f8ee68f92b9ea6f.2.jpg" img_width="160" keywords="Garden, Refurbished, Reception" lister_name="Ashley Milton" listing_type="let" price="500" price_coldrent="0" price_currency="GBP" price_formatted="500 GBP per week" price_high="500" price_low="500" price_type="weekly" property_type="flat" summary="Refurbished two double bedroom garden flat set in a period building with..." title="Flat to rent, London, NW3 - Garden" updated_in_days="1128.5" />
</response>

下面是我的用于检索xml数据并插入数据库的php代码:

<?php

$url = ("http://api.nestoria.co.uk/api?action=search_listings&centre_point=51.5424,-0.1734,2km&listing_type=rent&property_type=all&price_min=min&price_max=max&bedroom_min=0&bedroom_max=0&number_of_results=50&has_photo=1&page=4");

$xml = simplexml_load_file($url);
$latitude=array(-42.23, 42.23);
$longitude=array(-122.23, 122.23);

//use '%F' since it is float  signed/unsigned
$nodesNegV = $xml->xpath(sprintf('/response/listings[@latitude="%-F"]', $latitude[0]);
$nodesPosV = $xml->xpath(sprintf('/response/listings[@latitude="%F"]', $latitude[1]);

if (!empty($nodesNegV)) 
{
 printf('Latitude "%F" found which is negeative', $latitude[0]);
}  
else if(!empty($nodesPosV))
{
 printf('Latitude "%F" found which is positivetive', $latitude[1]);
}
else
{
echo "nothing found";
}

$nodesNegV = $xml->xpath(sprintf('/response/listings[@longitude="%-F"]', $longitude[0]);
$nodesPosV = $xml->xpath(sprintf('/response/listings[@longitude="%F"]', $longitude[1]);

if (!empty($nodesNegV)) 
{
 printf('Longitude "%F" found which is negeative', $longitude[0]);
}  
else if(!empty($nodesPosV))
{
 printf('Longitude "%F" found which is positivetive', $longitude[1]);
}
else
{
echo "nothing found";
}

foreach ($xml->response->listings as $entry) {


echo $entry->attributes()->bathroom_number;
echo $entry->attributes()->bedroom_number;
echo $entry->attributes()->datasource_name;
echo $entry->attributes()->guid;
echo $entry->attributes()->img_url;
echo $entry->attributes()->keywords;
echo $entry->attributes()->latitude;
echo $entry->attributes()->lister_name;
echo $entry->attributes()->listing_type;
echo $entry->attributes()->longitude;
echo $entry->attributes()->price;
echo $entry->attributes()->price_type;
echo $entry->attributes()->property_type;
echo $entry->attributes()->summary;
echo $entry->attributes()->title;

// Process XML file
// Opens a connection to a PostgresSQL server
$connection = pg_connect("dbname=postgis user=postgres password=local");
$query = "INSERT INTO nestoriaphp(bathroom, bedroom, datasource, guid, image, keywords, latitude, lister, listype, longitude, price, pricetype, property_type, summary, title) VALUES ('" . pg_escape_string($entry->attributes()->bathroom_number) . "', '" . pg_escape_string($entry->attributes()->bedroom_number) . "', '" . pg_escape_string($entry->attributes()->datasource_name) . "', '" . pg_escape_string($entry->attributes()->guid) . "', '" . pg_escape_string($entry->attributes()->img_url) ."', '" . pg_escape_string($entry->attributes()->keywords) . "', '" . pg_escape_string($entry->attributes()->latitude) . "', '" . pg_escape_string($entry->attributes()->lister_name) . "', '" . pg_escape_string($entry->attributes()->listing_type) . "', '" . pg_escape_string($entry->attributes()->longitude) . "', '" . pg_escape_string($entry->attributes()->price) . "', '" . pg_escape_string($entry->attributes()->price_type) ."', '" . pg_escape_string($entry->attributes()->property_type) . "', '" . pg_escape_string($entry->attributes()->summary) . "', '" . pg_escape_string($entry->attributes()->title) . "')";

$result = pg_query($query);

printf ("These values are inserted into the database - %s %s %s", $entry->attributes()->bathroom_number, $entry->attributes()->bedroom_number, $entry->attributes()->datasource_name, $entry->attributes()->guid, $entry->attributes()->img_url, $entry->attributes()->keywords, $entry->attributes()->latitude, $entry->attributes()->lister_name, $entry->attributes()->listing_type, $entry->attributes()->longitude, $entry->attributes()->price, $entry->attributes()->price_type, $entry->attributes()->property_type, $entry->attributes()->summary, $entry->attributes()->title);

}
pg_close();

?>

2 个答案:

答案 0 :(得分:1)

使用sprintf返回根据格式化字符串格式生成的字符串。

由于您的latitdue /经度可以签名/取消,这里是解决方案

编辑每个%后面都可以跟+-,因此%-F代表负坐标,但保留%F unsigned是42.23

   $latitude=array(-42.23, 42.23);
   $longitude=array(-122.23, 122.23);

   //use '%F' since it is float  signed/unsigned
   $nodesNegV = $xml->xpath(sprintf('/response/listings[@latitude="%-F"]', $latitude[0]);
   $nodesPosV = $xml->xpath(sprintf('/response/listings[@latitude="%F"]', $latitude[1]);

    if (!empty($nodesNegV)) 
   {
     printf('Latitude "%-F" found which is negeative', $latitude[0]);
   }  
   else if(!empty($nodesPosV))
   {
     printf('Latitude "%F" found which is positivetive', $latitude[1]);
   }
  else
  {
    echo "nothing found";
  }

对经度使用相同的方法。

答案 1 :(得分:0)

您必须在加载的xml中检查每个属性是否存在。

这就是为什么我们必须始终针对有效的xml架构文档验证xml文件,以告知xml文档的每个元素中都存在所有属性。

我认为没有办法以任何其他方式做到这一点。

相关问题