SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert only selection from XML datafeed

    Hello Guys

    I've been working with XML datafeeds and I have a feed that holds 3,000 products.

    However, I only want to use products from the feed with 'dahon' in the name. This is only about 12 of the 3,000.

    Can I modify my PHP to only insert the products with 'dahon' in the name?

    Here's my code I'm using...

    PHP Code:
    $xml simplexml_load_file("foldingdatafeed_79329.xml");

    foreach(
    $xml->merchant->prod as $product){
    $productid $product->id;
    $name $product->text->name;
    $descrip $product->text->desc;


    $sql "INSERT INTO products (productid, productname, productdescription)
    VALUES ('
    $productid', '$name', '$descrip')"
    Any help would be appreciated

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The easiest way to do this would be to use an IF statement and the preg_match function, see the below example:

    PHP Code:
    if (preg_match('/dahon/i'$product->text->name)) {
        
    // Insert into database


  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Lets say you wanted to use *nix text tools to pull out those 12 records from the original xml file to a second, smaller file - how could it be done, anyone know?

    Maybe its not strictly a reply, just a musing on the same subject.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •