J Sharman said:
What is the easiest way to import an XML Datafeed from a URL
into a MYSQL Database? Possibly using PHP
Easiest in terms of what -- ease of coding, use of memory,
execution time, something else? What are the dimensions of the
task -- 3 kb daily or 4 Mb per minute? How far along are you in
deciding how the XML should be mapped onto the database structure?
Is this a one-time task or a repetitive routine?
Here are a few options, just to get you started:
A. The memory-saving, but slow, one
Get one XML entity equivalent to a database record at a time,
parse, and insert. Say, you have an XML entity:
<event id="387629">
<date>2003-07-12</date>
<desc>This event cannot be adequately described</desc>
</event>
which can be mapped to a database query:
INSERT INTO events SET
id = 387629,
date = '2003-07-12',
desc = 'This event cannot be adequately described';
Running one query at a time will be slow because of the fixed
per-query overhead.
B. A faster one
Get a bunch of record equivalents at a time, parse them,
store the data as CSV, and then use LOAD DATA INFILE.
Using the <event> entity from the example above, you could
have a CSV file like this:
387629,"2003-07-12","This event cannot be adequately described"
387632,"2003-07-14","Just a non-descript event"
[more records like this]
786523,"2003-07-23","This is the last event"
MySQL will gladly (and VERY quickly) swallow this.
Here, the bottleneck will be writing records into the file.
C. A very fast (and memory-hungry) one
Get all record equivalents you want, parse them, and combine
the results into one monstrous query like this:
INSERT INTO events (id, date, desc) VALUES (
(387629,'2003-07-12','This event cannot be adequately described'),
(387632,'2003-07-14','Just a non-descript event'),
[more records like this]
(786523,'2003-07-23','This is the last event')
);
Everything wraps up in one query, but you risk running out of
memory if there is a lot of data. There is a workaround,
however; run the query every time the query string exceeds
certain length, and then begin to compose a new one.
Essentially, you'd be doing block writes...
Cheers,
NC