Forum Moderators: coopster
<html>
<head>
<title>PHP'S DOMDocument, So Easy A Caveman Can Do It!</title>
</head>
<body>
<?php
$xml_file = "xmlfile.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false; //ignore useless childNodes which are just blank space
$xmlDoc->load($xml_file);
$readyMadeDeal_nodes = $xmlDoc->getElementsByTagName('ReadyMadeDeals'); //all the nodes by name of 'ReadyMadeDeals'
//each $item in the loop will be one of the 'ReadyMadeDeal' nodes:
foreach ($readyMadeDeal_nodes as $item) {
foreach ($item->childNodes as $RMDnode) { //each $RMDnode will be one of the 'RMD' nodes
//get the 'ref' attribute of the RMD node, if desired
echo '<small>property listing id #'.$RMDnode->getAttribute('ref').'</small><br>';
//get the 'FullAddress' and 'PropertyInfo' nodes from the 'RMD' nodes' childNode's:
foreach ($RMDnode->childNodes as $RMDchild) {
switch($RMDchild->nodeName) {
case 'FullAddress':
echo '<h2 style="margin:0;">'.$RMDchild->nodeValue.'</h2>';
break;
case 'PropertyInfo':
echo '<p style="margin-top:0;"><i>'.$RMDchild->nodeValue.'</i></p>';
break;
}
}
}
}
?>
</body>
</html>
Thanks
How would I from there go about storing that information into my MySql Database?
<?php
//make your connection to mysql
$sql = mysql_connect("localhost", "username", "password") || die('Unable To Connect! '.mysql_error());
//set your database name
mysql_select_db("database_name_here", $sql);
$xml_file = "xmlfile.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false; //ignore useless childNodes which are just blank space
$xmlDoc->load($xml_file);
$readyMadeDeal_nodes = $xmlDoc->getElementsByTagName('ReadyMadeDeals'); //all the nodes by name of 'ReadyMadeDeals'
//each $item in the loop will be one of the 'ReadyMadeDeal' nodes:
foreach ($readyMadeDeal_nodes as $item) {
foreach ($item->childNodes as $RMDnode) { //each $RMDnode will be one of the 'RMD' nodes
//get the 'ref' attribute of the RMD node, if desired
$ref = mysql_real_escape_string($RMDnode->getAttribute('ref'), $sql);
echo '<small>property listing id #'.$RMDnode->getAttribute('ref').'</small><br>';
//set a couple of 'clean' holders to re-use for insertion later:
$fullAddress = '';
$propertyInfo = '';
//get the 'FullAddress' and 'PropertyInfo' nodes from the 'RMD' nodes' childNode's:
foreach ($RMDnode->childNodes as $RMDchild) {
switch($RMDchild->nodeName) {
case 'FullAddress':
$fullAddress = mysql_real_escape_string($RMDchild->nodeValue, $sql);
echo '<h2 style="margin:0;">'.$RMDchild->nodeValue.'</h2>';
break;
case 'PropertyInfo':
$propertyInfo = mysql_real_escape_string($RMDchild->nodeValue, $sql);
echo '<p style="margin-top:0;"><i>'.$RMDchild->nodeValue.'</i></p>';
break;
}
}
$query = mysql_query("INSERT INTO properties (ref, FullAddress, PropertyInfo) VALUES ($ref, $fullAddress, $propertyInfo)") || die('Failed Query');
}
}
?>
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/sites/#*$!x/public_html/xml.php on line 9
Fatal error: Call to undefined method DOMText::getAttribute() in /home/sites/#*$!x/public_html/xml.php on line 22
<html>
<head>
<title>PHP'S DOMDocument, And Inserting Values To Database From XML.</title>
</head>
<body>
<?php
//>
$sql = mysql_connect("localhost", "usernameHere", "passwordHere"); //adjust username, password as needed...
if (!$sql) {
die('Unable To Connect! '.mysql_error());
}
mysql_select_db("RealEstateListings", $sql); //set to name of your database
$xml_file = "xmlfile.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false; //ignore useless childNodes which are just blank space
$xmlDoc->load($xml_file);
$readyMadeDeal_nodes = $xmlDoc->getElementsByTagName('ReadyMadeDeals'); //all the nodes by name of 'ReadyMadeDeals'
//each $item in the loop will be one of the 'ReadyMadeDeal' nodes:
foreach ($readyMadeDeal_nodes as $item) {
foreach ($item->childNodes as $RMDnode) { //each $RMDnode will be one of the 'RMD' nodes
if ($RMDnode->nodeType === 1) {
//get the 'ref' attribute of the RMD node, if desired
$ref = $RMDnode->getAttribute('ref'); //not using mysql_real_escape_string here, assuming will be int, will check later
echo '<small>property listing id #'.$ref.'</small><br>';
//set a couple of 'clean' holders to re-use for insertion later:
$address = '';
$info = '';
//get the 'FullAddress' and 'PropertyInfo' nodes from the 'RMD' nodes' childNode's:
foreach ($RMDnode->childNodes as $RMDchild) {
if ($RMDchild->nodeType === 1) {
switch($RMDchild->nodeName) {
case 'FullAddress':
$addr = $RMDchild->nodeValue;
$address = mysql_real_escape_string($addr, $sql); //prevent mysql injection, database ready
echo '<h2 style="margin:0;">'.$addr.'</h2>';
break;
case 'PropertyInfo':
$inf = $RMDchild->nodeValue;
$info = mysql_real_escape_string($inf, $sql); //database ready
echo '<p style="margin-top:0;"><i>'.$inf.'</i></p>';
break;
}
}
}
if (is_numeric($ref) && strlen($address) && strlen($inf)) { //we have valid data to work with
$ref = (int)$ref; //cast as integer
//in setting up this sample, I'm assuming the use of a UNIQUE constraint
//on the ref column of the database table, to avoid erroneous duplicate entries.
//needs the quotes on $address & $info, was tripping me up otherwise.
$query = mysql_query("INSERT INTO properties (ref, FullAddress, PropertyInfo) VALUES ($ref,'$address','$info')", $sql);
if (!$query) {
$err = mysql_error();
if (preg_match("/Duplicate Entry/i", $err)) { //this will show if you try re-running with the same xml file:
echo '<p style="color:red;">'.$err.' , query was aborted</p>';
} else {
die('<p style="color:red;">Failed Query!<br>'.$err.'</p>');
}
}
} else {
echo '<p style="color:red;">not is_numeric and all that jazz, query not attempted</p>';
}
}//end if $RMDnode->nodeType
}
}
?>
</body>
</html>
<?php
//>
$sql = mysql_connect("localhost", "root");
if (!$sql) {
die('Unable To Connect! '.mysql_error());
}
if (mysql_query("CREATE DATABASE RealEstateListings DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",$sql)) {
echo '<p>Database created</p>';
mysql_select_db("RealEstateListings", $sql);
$query = "CREATE TABLE properties (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), ref int NOT NULL, UNIQUE(ref), FullAddress varchar(255), PropertyInfo TEXT)";
$created = mysql_query($query,$sql);
if ($created) {
echo '<p>Successfully created database with table properties</p>';
} else {
die('Failed Query: '.mysql_error());
}
mysql_close($sql);
} else {
die('Error creating database: '.mysql_error());
}
?>
<?xml version="1.0"?>
<FeedData>
<ReadyMadeDeals>
<RMD ref="11085">
<FullAddress>10 Test Street, NE3 33S</FullAddress>
<PropertyInfo>A 2 Bed property in excellent condition with a large rear garden and parking for 2 cars. UPVC windows throughout.</PropertyInfo>
</RMD>
</ReadyMadeDeals>
<ReadyMadeDeals>
<RMD ref="22099">
<FullAddress>22 Test2 Street, SW3 33S</FullAddress>
<PropertyInfo>A 4 Bed property in excellent condition with parking for 2.2 cars. Includes windows, walls, and a roof!</PropertyInfo>
</RMD>
</ReadyMadeDeals>
</FeedData>
[edited by: jatar_k at 8:33 pm (utc) on Mar 4, 2010]
[edit reason] no email addresses thanks [/edit]