2012-03-10 4 views
1

일부 하위 요소에 부재 속성을 포함하는 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&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(); 

?> 

답변

0

로드 된 XML에 존재하는 각 속성을 확인해야합니다.

우리는 XML 문서의 각 요소에 모든 특성이 있음을 알기 위해 유효한 xml 스키마 문서에 대해 xml 파일의 유효성을 검사해야합니다.

나는 다른 어떤 쉬운 방법으로 이것을 할 어떤 방법이 있다고 생각하지 않는다.

+0

그리고이 상황에서 유효성 검사를 어떻게 수행합니까? – akinboj

+0

나는 donr이 방법을 알고 있지만 이것이 도움이된다고 생각합니다. http://etutorials.org/Server+Administration/upgrading+php+5/Chapter+5.+XML/5.11+Validating+Against+a+Schema/. 또한 xsd를 먼저 생성 한 다음 코드가 xsd에 지정된 모든 속성에 대해 XML을 검증합니다. – Dinesh

1

형식 문자열 형식에 따라 생성 된 문자열을 반환하는 sprintf를 사용하십시오. 당신의 latitdue/경도에 서명 할 수 있기 때문에/여기 unsgined

%+ 또는 -, 음의 좌표 따라서 %-F 뒤에 만 인 서명에 대한 %F을 유지 할 수 있습니다 솔루션

EDIT입니다 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

+0

xml 확인을 반복하기 전에이 메서드는 XPath'$ xml-> 경로 (....)를 설정하기 위해 이름/특성에 대한 사전 지식이 필요합니다. ' – jmishra

+0

이 오류가 발생합니다 : 구문 분석 오류 : 예기치 않은 구문 오류 ';' 줄 10에있는 C : \ XAMMP \ ... \ nestoriauk.php에 다음 줄이 있습니다. $ nodesNegV = $ xml-> xpath (sprintf ('/ 응답/목록 [@latitude = "% F"]', $ 위도 [0]); – akinboj

관련 문제