sess4561 Posted March 5, 2010 Share Posted March 5, 2010 Hi guys I was wondering if anyone could help me with the following code. I am trying to parse an XML feed, and insert the information into my Mysql database. So far its taken me nearly a week, and I have it almost complete, but struggling at a few points as shown below:- THIS IS AN EXAMPLE OF THE XML FEED <?xml version="1.0" encoding="utf-8"?> <FeedData> <ReadyMadeDeals> <RMD ref="11609"> <FullAddress>19 Billingham Road, St Helens, Merseyside, WA9 5LS</FullAddress> <ShortAddress>St Helens, Merseyside, WA9</ShortAddress> <Beds>2</Beds> <Type>Terrace - Mid</Type> <PropertyInfo>Two bedroom mid terraced property, UPVC double glazed, gardens to front and rear</PropertyInfo> <GeneralComments>Rent per month £449.00 mortgage payment per month £449.00 Cashflow £150.00 per month £2,000.00 mortgage deposit required</GeneralComments> <Financial> <PriceToBuy>80000</PriceToBuy> <Valuation>104000</Valuation> <Equity>24000</Equity> <BMV>23</BMV> <ValuationRentPCM>449</ValuationRentPCM> <CashFlowPCM>150</CashFlowPCM> </Financial> <Photos> <Values id="main" image="http://xxx.com/WebDocs1/Photos/FILE_EXTN111609-12684847.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_EXTN111609-12684847.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_KITCH11609-90971941.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_KITCH11609-90971941.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_BATHR11609-24383553.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_BATHR11609-24383553.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_LOUNG11609-98080636.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_LOUNG11609-98080636.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_OTHER11609-67713664.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_OTHER11609-67713664.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_OTHR211609-78084668.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_OTHR211609-78084668.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_OTHR311609-45538567.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_OTHR311609-45538567.jpg" /> <Values image="http://xxx.com/WebDocs1/Photos/FILE_OTHR411609-70934208.jpg" thumbnail="http://xxx.com/WebDocs1/Photos/thb/FILE_OTHR411609-70934208.jpg" /> </Photos> <PropertyCondition> <Values area="Driveway / Gardens" rating="4" comments="Brick paved garden area to front, block paved garden to rear, roadside parking, front wall could be removed to create driveway" /> <Values area="Exterior of Property" rating="4" comments="Built 1964, neat and tidy terraced property in row of four houses in quiet cul de sac" /> <Values area="Conservatory" /> <Values area="UPVC/Windows/Doors" rating="5" comments="UPVC double glazed windows and doors throughout" /> <Values area="Garage" /> <Values area="Roof/Gutter" rating="4" comments="All in good condition" /> <Values area="Porch/Hallway" /> <Values area="Living Room" rating="3" comments="Carpets in good condition, decor ok, gas fire" /> <Values area="Dining Room" rating="3" comments="Carpets in good condition, decor ok, gas wall heater" /> <Values area="Stairs & Landing" rating="3" comments="Needs new carpet, decor ok" /> <Values area="Kitchen" rating="3" comments="Vinyl flooring in fair condition, decor ok" /> <Values area="Utility/Other" /> <Values area="Bedroom 1" rating="2" comments="Double room, needs new carpets and redecoration" /> <Values area="Bedroom 2" rating="2" comments="Double room, needs new carpets and redecoration" /> <Values area="Bedroom 3" /> <Values area="Main Bathroom" rating="3" comments="Carpets in ok condition, tiled walls, bath over shower, decor ok" /> <Values area="Central Heating Boiler" rating="1" comments="No central heating in property" /> <Values area="Radiators" rating="1" comments="No central heating in property" /> <Values area="General Decoration & Refurbishment" rating="3" comments="Property will benefit from gas central heating and some redecoration and new carpets to some rooms" /> </PropertyCondition> </RMD> MY PHP CODE IS AS BELOW <?php //> $sql = mysql_connect("localhost", "xxx", "xxx"); //adjust username, password as needed... if (!$sql) { die('Unable To Connect! '.mysql_error()); } mysql_select_db("xxx", $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 = ''; $shortaddress = ''; $beds = ''; $type = ''; $info = ''; $comments = ''; $investorsprice = ''; //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 'ShortAddress': $saddr = $RMDchild->nodeValue; $shortaddress = mysql_real_escape_string($saddr, $sql); //database ready echo '<p style="margin-top:0;"><i>'.$saddr.'</i></p>'; break; case 'Beds': $bed = $RMDchild->nodeValue; $beds = mysql_real_escape_string($bed, $sql); //database ready echo '<p style="margin-top:0;"><i>'.$bed.'</i></p>'; break; case 'Type': $typ = $RMDchild->nodeValue; $type = mysql_real_escape_string($typ, $sql); //database ready echo '<p style="margin-top:0;"><i>'.$typ.'</i></p>'; 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; case 'GeneralComments': $coms = $RMDchild->nodeValue; $comments = mysql_real_escape_string($coms, $sql); //database ready echo '<p style="margin-top:0;"><i>'.$coms.'</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 deals (ref, FullAddress, ShortAddress, Beds, Type, PropertyInfo, GeneralComments, PriceToInvestor) VALUES ('$ref','$address','$shortaddress','$beds','$type','$info','$comments','$investorsprice')", $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 } } ?> This code works absolutely fine, up until the point of inserting data from the <financial> elements. <Financial> <PriceToBuy>103950</PriceToBuy> <Valuation>135000</Valuation> <Equity>31050</Equity> <ValuationRentPCM>478</ValuationRentPCM> </Financial> My question is, how do I reference something that is inside of <Financial>? I so far have referenced everything with:- 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; etc, etc, but if I try to reference 'Equity' for example(as shown below) it doesnt work:- case 'Equity': $eqt = $RMDchild->nodeValue; $equity = mysql_real_escape_string($eqt, $sql); //database ready echo '<p style="margin-top:0;"><i>'.$eqt.'</i></p>'; break; Also later on in the xml there is a <photos> section, etc. Again, how would I reference these, to store them into mySQL? <Photos> <Values id="main" image="http://#*$!.com/WebDocs1/Photos/FILE_EXTN111193-73804565.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_EXTN111193-73804565.jpg" /> <Values image="http://#*$!.com/WebDocs1/Photos/FILE_KITCH11193-98107295.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_KITCH11193-98107295.jpg" /> <Values image="http://#*$!.com/WebDocs1/Photos/FILE_BATHR11193-52689696.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_BATHR11193-52689696.jpg" /> <Values image="http://#*$!.com/WebDocs1/Photos/FILE_OTHER11193-28990032.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_OTHER11193-28990032.jpg" /> <Values image="http://#*$!.com/WebDocs1/Photos/FILE_OTHR211193-66753531.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_OTHR211193-66753531.jpg" /> <Values image="http://#*$!.com/WebDocs1/Photos/FILE_OTHR311193-35691211.jpg" thumbnail="http://#*$!.com/WebDocs1/Photos/thb/FILE_OTHR311193-35691211.jpg" /> </Photos> I hope someone can shed some light on this as its doing my head in! Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/194230-help-with-xml-parsing-code/ Share on other sites More sharing options...
sess4561 Posted March 5, 2010 Author Share Posted March 5, 2010 anyone? Link to comment https://forums.phpfreaks.com/topic/194230-help-with-xml-parsing-code/#findComment-1021896 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.