在xml中解析不存在的属性

问题描述:

我有一个xml输出,其中包含某些子元素的缺少属性。我如何解析并将它们插入到我的数据库中?上部元件具有属性“纬度”和“经度”,而第二元素不包含这些属性 见下面的XML结构的摘录:在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(); 

?> 

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

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

我不认为有任何其他简单的方法来做到这一点。

+0

我该如何在这种情况下进行验证? – akinboj 2012-03-10 09:06:51

+0

我真的不知道如何做到这一点,但认为这将有助于http://etutorials.org/Server+Administration/upgrading+php+5/Chapter+5.+XML/5.11+Validating+Against+a+Schema/。此外,需要首先创建xsd,然后您的代码将验证xml中xsd中指定的所有属性。 – Dinesh 2012-03-10 09:12:01

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

由于您latitdue /经度可以签署/这里unsgined是解决

编辑每个%可以跟随+-,因此%-F负坐标,但保持%F无符号是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"; 
    } 

对于经度使用相同的方法。

+0

请在代码的哪一部分插入您的代码? – akinboj 2012-03-10 09:34:34

+0

在迭代你的xml检查之前,你认为可能缺少这个名字,这个方法需要事先知道名称/属性才能设置XPath'$ xml-> path(....)' – jmishra 2012-03-10 09:41:15

+0

我保留出现此错误:解析错误:语法错误,意外的';'在第10行的C:\ XAMMP \ ... \ nestoriauk.php这是行:$ nodesNegV = $ xml-> xpath(sprintf('/ response/listings [@latitude =“%F”]',$纬度[0]); – akinboj 2012-03-10 09:45:59