I’m not confident this is the right way of getting the nodes, but this works. I’m much more comfortable with SimpleXML…sorry.
<?php
$oDOM = new DOMDocument();
$oDOM->loadXML(file_get_contents('books.xml')); #See: http://msdn.microsoft.com/en-us/library/ms762271(VS.85).aspx
foreach ($oDOM->getElementsByTagName('book') as $oBookNode)
{
printf(
"INSERT INTO table (title, author, description) VALUES ('%s', '%s', '%s')",
mysql_real_escape_string($oBookNode->getElementsByTagName('title')->item(0)->nodeValue),
mysql_real_escape_string($oBookNode->getElementsByTagName('author')->item(0)->nodeValue),
mysql_real_escape_string($oBookNode->getElementsByTagName('description')->item(0)->nodeValue)
);
}
/*
INSERT INTO table (title, author, description) VALUES ('XML Developer's Guide', 'Gambardella, Matthew', 'An in-depth look at creating applications with XML.')
INSERT INTO table (title, author, description) VALUES ('Midnight Rain', 'Ralls, Kim', 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.')
INSERT INTO table (title, author, description) VALUES ('Maeve Ascendant', 'Corets, Eva', 'After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.')
INSERT INTO table (title, author, description) VALUES ('Oberon's Legacy', 'Corets, Eva', 'In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.')
INSERT INTO table (title, author, description) VALUES ('The Sundered Grail', 'Corets, Eva', 'The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.')
INSERT INTO table (title, author, description) VALUES ('Lover Birds', 'Randall, Cynthia', 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.')
INSERT INTO table (title, author, description) VALUES ('Splish Splash', 'Thurman, Paula', 'A deep sea diver finds true love twenty thousand leagues beneath the sea.')
INSERT INTO table (title, author, description) VALUES ('Creepy Crawlies', 'Knorr, Stefan', 'An anthology of horror stories about roaches, centipedes, scorpions and other insects.')
INSERT INTO table (title, author, description) VALUES ('Paradox Lost', 'Kress, Peter', 'After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.')
INSERT INTO table (title, author, description) VALUES ('Microsoft .NET: The Programming Bible', 'O'Brien, Tim', 'Microsoft's .NET initiative is explored in detail in this deep programmer's reference.')
INSERT INTO table (title, author, description) VALUES ('MSXML3: A Comprehensive Guide', 'O'Brien, Tim', 'The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.')
INSERT INTO table (title, author, description) VALUES ('Visual Studio 7: A Comprehensive Guide', 'Galos, Mike', 'Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.')
*/
?>