Working with XML?

Discussion in 'PHP' started by adamjblakey, Sep 6, 2007.

  1. #1
    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
     
    adamjblakey, Sep 6, 2007 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    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?
     
    Colbyt, Sep 6, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    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.
     
    adamjblakey, Sep 6, 2007 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    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.
     
    Colbyt, Sep 6, 2007 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    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?
     
    adamjblakey, Sep 7, 2007 IP
  6. aRo`

    aRo` Peon

    Messages:
    141
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you are using php5, you should take a look at SimpleXML.

    Easy and fast way to parse XML files.
     
    aRo`, Sep 7, 2007 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    I am using php4 :(
     
    adamjblakey, Sep 7, 2007 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    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
     
    adamjblakey, Sep 10, 2007 IP
  9. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Someone has got to know how to work with this? Please..
     
    adamjblakey, Sep 10, 2007 IP
  10. Hjalle

    Hjalle Peon

    Messages:
    117
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Why don't you go for PHP5 and use SimpleXML? I believe your host has support for PHP5, most of them do
     
    Hjalle, Sep 10, 2007 IP
  11. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #11
    I cannot use PHP5 i have checked with my host and they do not support. I can not move either.
     
    adamjblakey, Sep 11, 2007 IP
  12. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #12
    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?
     
    adamjblakey, Sep 11, 2007 IP
  13. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #13
    $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.
     
    krt, Sep 11, 2007 IP