Note that there are some explanatory texts on larger screens.

plurals
  1. POparse absent attributes in xml
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;response ..... &lt;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" /&gt; &lt;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" /&gt; &lt;/response&gt; </code></pre> <p>Below is my php code for retrieving the xml data and inserting into database:</p> <pre><code>&lt;?php $url = ("http://api.nestoria.co.uk/api?action=search_listings&amp;centre_point=51.5424,-0.1734,2km&amp;listing_type=rent&amp;property_type=all&amp;price_min=min&amp;price_max=max&amp;bedroom_min=0&amp;bedroom_max=0&amp;number_of_results=50&amp;has_photo=1&amp;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-&gt;xpath(sprintf('/response/listings[@latitude="%-F"]', $latitude[0]); $nodesPosV = $xml-&gt;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-&gt;xpath(sprintf('/response/listings[@longitude="%-F"]', $longitude[0]); $nodesPosV = $xml-&gt;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-&gt;response-&gt;listings as $entry) { echo $entry-&gt;attributes()-&gt;bathroom_number; echo $entry-&gt;attributes()-&gt;bedroom_number; echo $entry-&gt;attributes()-&gt;datasource_name; echo $entry-&gt;attributes()-&gt;guid; echo $entry-&gt;attributes()-&gt;img_url; echo $entry-&gt;attributes()-&gt;keywords; echo $entry-&gt;attributes()-&gt;latitude; echo $entry-&gt;attributes()-&gt;lister_name; echo $entry-&gt;attributes()-&gt;listing_type; echo $entry-&gt;attributes()-&gt;longitude; echo $entry-&gt;attributes()-&gt;price; echo $entry-&gt;attributes()-&gt;price_type; echo $entry-&gt;attributes()-&gt;property_type; echo $entry-&gt;attributes()-&gt;summary; echo $entry-&gt;attributes()-&gt;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-&gt;attributes()-&gt;bathroom_number) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;bedroom_number) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;datasource_name) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;guid) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;img_url) ."', '" . pg_escape_string($entry-&gt;attributes()-&gt;keywords) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;latitude) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;lister_name) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;listing_type) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;longitude) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;price) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;price_type) ."', '" . pg_escape_string($entry-&gt;attributes()-&gt;property_type) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;summary) . "', '" . pg_escape_string($entry-&gt;attributes()-&gt;title) . "')"; $result = pg_query($query); printf ("These values are inserted into the database - %s %s %s", $entry-&gt;attributes()-&gt;bathroom_number, $entry-&gt;attributes()-&gt;bedroom_number, $entry-&gt;attributes()-&gt;datasource_name, $entry-&gt;attributes()-&gt;guid, $entry-&gt;attributes()-&gt;img_url, $entry-&gt;attributes()-&gt;keywords, $entry-&gt;attributes()-&gt;latitude, $entry-&gt;attributes()-&gt;lister_name, $entry-&gt;attributes()-&gt;listing_type, $entry-&gt;attributes()-&gt;longitude, $entry-&gt;attributes()-&gt;price, $entry-&gt;attributes()-&gt;price_type, $entry-&gt;attributes()-&gt;property_type, $entry-&gt;attributes()-&gt;summary, $entry-&gt;attributes()-&gt;title); } pg_close(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload