일부 하위 요소에 부재 속성을 포함하는 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 코드입니다 : 상단 요소는 속성 "위도"와 "경도"이러한 특성을 포함하지 않는 두 번째 요소는 아래의 XML 구조의 발췌를 참조 동안있다
<?php
$url = ("http://api.nestoria.co.uk/api?action=search_listings¢re_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();
?>
그리고이 상황에서 유효성 검사를 어떻게 수행합니까? – akinboj
나는 donr이 방법을 알고 있지만 이것이 도움이된다고 생각합니다. http://etutorials.org/Server+Administration/upgrading+php+5/Chapter+5.+XML/5.11+Validating+Against+a+Schema/. 또한 xsd를 먼저 생성 한 다음 코드가 xsd에 지정된 모든 속성에 대해 XML을 검증합니다. – Dinesh