Hi, I need to be able to search an XML database but unsure on how to do so. Example data from the database is: - <Property> <ID>14209</ID> <Partner_Id>15</Partner_Id> - <Name> - <![CDATA[ ]]> </Name> - <Reference> - <![CDATA[ NOBR15 ]]> </Reference> <Country>Portugal</Country> - <Region> - <![CDATA[ Madeira ]]> </Region> - <Location1> - <![CDATA[ Funchal ]]> </Location1> - <Location2> - <![CDATA[ ]]> </Location2> <Type>Villa</Type> <New_Development>0</New_Development> <Leaseback>0</Leaseback> <Investment>N</Investment> <Price>404750.00</Price> <Currency>GBP</Currency> <Bedrooms>5</Bedrooms> <Delivery_Date /> - <Short_Description> - <![CDATA[ ]]> </Short_Description> - <Long_Description> - <![CDATA[ A TRUE RURAL RETREAT IN A TRANQUIL SETTING AMONGST LUSH VEGETATION ! Split level Quinta /Vila - on the Funchal Border - only 12km away from Funchal and 600m above sea level with breathtaking sea and mountainviews ! Only 4 years old ! Large 7070 m2 grounds Building rights for at least another dwelling to be built on the land - eg. granny cottage or another residential home. Type: Residential Style: 2 Story Split "Modern Upmarket Vila Design" Bedrooms: 4 "WITH BUILT IN CUPBOARDS" Bathrooms: 3 "MAIN EN-SUITE" Garage: Triple "LOADS OF EXTRA PARKING" Basement: No Size: 495 m² Lot Type: Rectangular Lot Size: 7070 m² n/a "WITH PERMISSION FOR BUILDING ANOTHER HOME ON STAND" Has Suite: Yes Year Built: 2000 "AS NEW" Taxes: €0.00 EUR Condo Fees: €0.00 EUR ]]> </Long_Description> - <Small_Image_URL> - <![CDATA[ http://www.nobregarealty.com/Shared/Cache/Listing/114108/Photo/7-Gallery.img?_Version=632208272672200000 ]]> </Small_Image_URL> - <Large_Image_URL> - <![CDATA[ http://www.nobregarealty.com/Shared/Cache/Listing/114108/Photo/7-Gallery.img?_Version=632208272672200000 ]]> </Large_Image_URL> <Extra_Images /> - <Currencies> - <Currency> <Name>EUR</Name> <Price>599097.10</Price> </Currency> - <Currency> <Name>GBP</Name> <Price>404750.00</Price> </Currency> - <Currency> <Name>USD</Name> <Price>754848.94</Price> </Currency> </Currencies> </Property> Code (markup): So what i need to do is be able to search each field like an advanced search. I have only ever worked with SQL before so unsure on how to do this? Adam
What you have shown above is an "xml dump". MS Excel can open that. I am sure that are some freeware or shareware softwares that will open it also. Are you wanting to write a web script to perform these searches?
Yes i want to put a form with e.g. a country and bedrooms fields. When this form is submitted it searches the XML database for anything that has the same as $_post['country'] and $_post['bedrooms'] and returns the results.
I am currently searching for a way to extract some information from an xml file also. I have a couple of things bookmarked to check asap. If I find anything interesting I will either post the link or PM you. I read one this morning on how to extract the data to html using an xsl stylesheet. Most of it was over my head for a first reading. If you can get it to html, php should be easy. Here is the link. If it makes more sense to you, let me know.
I know how to access the data and extract but what is troubling me is extracting based criteria. e.g. with sql i could just do $query = "SELECT * FROM table WHERE location = '$_POST[location]'"; But i cannot do that with XML so i am wondering how i would go about that?
Anyone else offer some help on this? I have found a class that i can work with here: http://www.shop24-7.info/32-0-simplexml-alternative-php4.html But what i need to know is how to work with this bit here: $xml_parser = new sxml; $src=implode ('', file ($url)); $xml_parser->parse($src); $xml=$xml_parser->data; print_r($xml); Code (markup): I need to know how to add a condition to this e.g. print $XML where $_POST[country] = country
Why don't you go for PHP5 and use SimpleXML? I believe your host has support for PHP5, most of them do
Right i have now come up with this which should work, <?php //Create an XML parser $xml_parser = xml_parser_create(); // Set the functions to handle opening and closing tags xml_set_element_handler($xml_parser, "startElement", "endElement"); // Set the function to handle blocks of character data xml_set_character_data_handler($xml_parser, "characterData"); // Open the XML file for reading $xml = fopen("./database/data.xml","r") or die("Error reading XML data."); // Close the XML file fclose($xml); // Free up memory used by the XML parser xml_parser_free($xml_parser); function xmlfilter ($xml, $country, $type, $bedrooms) { $res = array(); foreach ($xml->widget as $w) { $keep = 1; if ($country!='') { if ((string)$w->country != $country) $keep = 0; } if ($type) { if ((int)$w->type > $type) $keep = 0; } if ($bedrooms) { if ((int)$w->bedrooms > $bedrooms) $keep = 0; } if ($keep) $res[] = $w; } return $res; } if (isset($_GET['sub'])) { $country = isset($_GET['country'])? $_GET['country'] : ''; $type = $_GET['type']; $bedrooms = $_GET['bedrooms']; $filtered = xmlfilter($xml ,$country, $type, $bedrooms); // pass xml echo '<pre>', print_r($filtered, true), '</pre>'; } ?> <form> country <input type="text" name="country" size="5"><br/> type <input type="text" name="type" size="5"><br/> bedrooms <input type="text" name="bedrooms" size="5"><br/> <input type="submit" name="sub" value="Submit"> </form> Code (markup): But i am getting the following error: Warning: Invalid argument supplied for foreach() which refers to this line. foreach ($xml->widget as $w) Code (markup): Any ideas what i have done wrong?
$xml in your code is just a string containing the XML, not an object with a "widget" property, it seems you want to parse $xml but have not included the code to do so. Have a look at http://www.php.net/manual/en/function.xml-parse.php and the notes for practical solutions.