douna3367
douna3367
2012-03-10 08:50

解析xml中缺少的属性

I have an xml output that contains absent attributes for some of the child elements. How can I parse and insert them into my database? The upper element has attributes "latitude" and "longitude" while the second element does not contain these attributes See an excerpt of the xml structure below:

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

Below is my php code for retrieving the xml data and inserting into database:

<?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条回答

  • dtt2012 dtt2012 9年前

    Use sprintf which returns a string produced according to the formatting string format.

    Since your latitdue/longitude could be signed/unsgined here is the solution

    EDIT Each % can be followed by + or -, hence %-F for negative coordinates but keep %F for unsigned which is 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";
      }
    

    Use the same method for longitude.

    点赞 评论 复制链接分享
  • dongwuge6201 dongwuge6201 9年前

    You will have to check each attribute for existense in the loaded xml.

    This is why we must always validate an xml file against a valid xml schema document to tell that all the attributes exists in each element of the xml document.

    I dont think there is any way to do this any other easy way.

    点赞 评论 复制链接分享

相关推荐