Storing XML data in MySQL database

Discussion in 'PHP' started by egdcltd, Feb 8, 2010.

  1. #1
    Okay, I want to extract information from an XML feed hosted elsewhere, using PHP, and then add the information to a MySQL database. I'd be running the script automatically once per day I think, adding any new entries and updating one field in every existing entry. Although this last may work better being run as a separate script. Problem is, I know nothing whatsoever about XML so I don't know where to start.

    Can anyone point me in the right direction please?
     
    egdcltd, Feb 8, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    One possible method, is regular expressions.
     
    danx10, Feb 8, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You can easily write a script to parse it using simplexml. You can load the xml into an object / array, and then loop through and add to your database as needed. Put this in a cron script or create a manual upload or something similar to run it every week.

    http://php.net/manual/en/function.simplexml-load-string.php

    Use var_dump or print_r to debug the specific xml.
     
    jestep, Feb 9, 2010 IP
  4. roy.laurie

    roy.laurie Peon

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yup, SimpleXML or XMLReader if you're working with large files. Depending on your schema structure, you might consider just using a simple key,value,row id, file id for each value you insert into the MySQL db.
     
    roy.laurie, Feb 9, 2010 IP
  5. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, XMLReader seems to be doing the trick. The file has over 1,300 entries, so I thought this way would be best.
     
    egdcltd, Feb 12, 2010 IP