Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Location
    New Zealand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Using PHP to parse an XML feed and commit to MySQL DB

    Hi all,

    My situation is this:
    I am using a CMS to create a classifieds website.
    A third party has created a web interface which users can use to create listings. That third party takes the listing information, and spits it out as an XML feed to various auction and classifieds websites.

    As a person trying to take advantage of this tool, I need to find a way to take this XML feed, and parse it in such a way that it can be committed to a MySQL database.

    I was thinking the best way of doing this would be to create a PHP script which pulls the feed, does some magic, and commits it to a MySQL DB.

    I would sincerely appreciate any assistance which can be provided.

    Kind regards,
    LB

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    have you got an example of the XML feed?
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    i assume you know how to handle arrays and/or objects
    PHP Code:
    // $xmlString = file_get_contents('feed's url');
    // this is what you would use to fetch actual xml file from your provider
    //next var is just a demonstration
    $xmlString ='
    <root>
    <Formula>
    <something>
        <blah>
        <foo>test1</foo>
        <foo>test2</foo>
        </blah>
    </something>
    <formulaname>Basic</formulaname>
    <movespeed>1</movespeed>
    <box>4</box>
    <chicken>3</chicken>
    <ducks>1</ducks>
    <cereal>2</cereal>
    </Formula>
    </root>'
    ;

    //if you feel comfortable with objects
    $xmlObject simplexml_load_string($xmlString);
    print_r($xmlObject);

    ///if you want it as array:
    $xmlarray json_decode(json_encode($xmlObject), 1);
    print_r($xmlarray); 
    having that you just have to take variables you want

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • Users who have thanked patryk for this post:

    light bulb (04-06-2013)

  • #4
    New to the CF scene
    Join Date
    Apr 2013
    Location
    New Zealand
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Many thanks!! That's enough to get me started. Unfortunately the XML feed contains multi-dimensional arrays up to 10 dimensions. When the unformatted feed was displayed on my monitor it was a bit of a jaw dropping "what the faff do I do" moment.

    Many thanks for your assistance

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If there is only specific things you need to retrieve from the document, why not just use xpath in combination with either the dom or simplexml parser? Can't really suggest what to put in it though since you don't have an example of the feed nor what you are trying to get out of it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

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