I am sorta new to MySQL and not 100% sure of what I can and can not do with it yet. I have an xml file with course information such as ReferenceNo. CourseName, CourseType, CourseLocation, and I want to load in into a database table that has column names such as ID, post_title, term_taxonomy_id etc

I am updating the site by uploading the xml to MySQL (I am using Worpress by the way). The table I am loading it into has different named columns to that of the elements I want to load into it. So for instance I want to load the
ReferenceNo on the xml into the ID column in the database table or the
CourseName on the xml into a column called post_title n the database table

At the moment I am just doing a good ol manual"find and replace" before uploadig it to change the field names to match the column names, but this is not a long term solution. Is there a way to assign the elements of the xml to the db table columns without having to change names to match??

Member Avatar

LOAD XML statement and use the SET clause. I'll see if I can dig out an example.

Member Avatar

OK here's the PHP/MySQL:

$db = new PDO("mysql:host=localhost;dbname=somedb",'xxxxx','xxxxxxxx', array(PDO::MYSQL_ATTR_LOCAL_INFILE => true));

$query = "LOAD XML LOCAL INFILE 'xml.xml'
SET character = role,
    title = film,
    type_of_part = part";

$stmt = $db->prepare($query);

As you can see the file was called xml.xml. Original I know.
Here's the xml I filched from http://www.databasejournal.com/features/mysql/all-about-the-mysql-5.5-load-xml-inline-statement.html

<?xml version=1.0"?>
    <role>Dirty Harry</role>
    <actor>Clint Eastwood</actor>
    <film>Dirty Harry</film>
    <role>Indiana Jones</role>
    <actor>Harrison Ford</actor>
    <film>Raiders of the Lost Ark</film>
    <role>Ethan Hunt</role>
    <actor>Tom Cruise</actor>
    <film>Mission Impossible</film>

You just need to map the fieldnames to the tagnames in the SET clause.

Hi Diafol,

Thanks for your reply. I have applied the SET clause like so:

LOAD XML LOCAL INFILE 'C:/courses.xml' 
SET ID = ReferenceNo,
    post_title = CourseName,
    post_type = 'course';

And I get the following error:

1054 - Unknown column 'ReferenceNo' in 'field list'

This is a snippet of the xml

<?xml version="1.0" encoding="utf-8"?>
    <ProviderName>name deleted</ProviderName>
    <CourseName>IT Maintenance - Short Course</CourseName>
    <LocationName>name deleted </LocationName>
commented: I'd hit it +14

The last thing I am trying to do is "Waste everybodies time", my sincere apologies if this is how I have come across. If anything I am overly-conscious of the time someone offers and do not want to keep badgering them once they have taken the time to help.

I have being reprimanded, lesson learnt thanks for the heads up.

Slight tweak and its working... thanks Diafol :/

LOAD XML LOCAL INFILE 'C:/courses.xml' 
(@ReferenceNo, @ProviderCode, @ProviderName, @CourseID, @CourseName, @LocationCode, @LocationName, @StartDate, @FinishDate)
SET ID = @ReferenceNo,
    post_title = @CourseName,
    post_type = 'course';
commented: Great! Thanks for sharing +15
Member Avatar

Thank you for coming back with the solution, I'm sure that this will help a lot of people. I wasn't aware of the need to "var out" the tagnames. We learn something new every day :)