Hi
I need to insert the data from an XML feed into a MySQL database. I have been working with simple XML to try to make this work.
<?php
class XMLParser{
private $xml;
public function __construct($xmlFile='default_xml_file.xml'){
if(!file_exists($xmlFile)){
throw new Exception('Invalid XML file.');
}
// read XML file
if(!$this->xml=simplexml_load_file($xmlFile)){
throw new Exception('Error reading XML file.');
}
}
public function fetchNodesAsObjects(){
$nodes=array();
foreach($this->xml as $node){
$nodes[]=$node;
}
return $nodes;
}
}
try{
$xmlPar=new XMLParser('test.xml');
$nodes=$xmlPar->fetchNodesAsObjects();
echo "<pre>";
print_r($nodes); // ECHOS OUT THE ARRAY TREE PERFECTLY
echo "</pre>";
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
Resulting Array:
Array
(
[0] => SimpleXMLElement Object
(
[feed_version] => 2
)
[1] => SimpleXMLElement Object
(
[id] => 179
[name] => Test
[email] => info@test.com
[tel] => SimpleXMLElement Object
(
)
[mob] => SimpleXMLElement Object
(
)
[addr1] => SimpleXMLElement Object
(
)
[addr2] => SimpleXMLElement Object
(
)
[town] => SimpleXMLElement Object
(
)
[region] => SimpleXMLElement Object
(
)
[postcode] => SimpleXMLElement Object
(
)
[country] => SimpleXMLElement Object
(
)
[logo] => SimpleXMLElement Object
(
)
)
[2] => SimpleXMLElement Object
(
[id] => 34935
[date] => 2009-11-03 06:52:45
[ref] => ABC0108
[price] => 450000
[currency] => EUR
[price_freq] => sale
[part_ownership] => 0
[leasehold] => 0
[type] => SimpleXMLElement Object
(
[en] => Plot
)
[town] => Cardiff
[province] => Wales
[location_detail] => SimpleXMLElement Object
(
)
[beds] => 0
[baths] => 0
[pool] => 0
=> SimpleXMLElement Object
(
)
[desc] => SimpleXMLElement Object
(
[en] =>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat. Ut wisis enim ad minim veniam, quis nostrud exerci tution ullam corper suscipit lobortis nisi ut aliquip ex ea commodo consequat. Duis te feugi facilisi. Duis autem dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit au gue duis dolore te feugat nulla facilisi.
)
)
[3] => SimpleXMLElement Object
(
[id] => 34999
[date] => 2009-11-10 06:52:45
[ref] => ABC2345
[price] => 250000
[currency] => EUR
[price_freq] => sale
[part_ownership] => 0
[leasehold] => 0
[type] => SimpleXMLElement Object
(
[en] => Plot
)
[town] => Ffestiniog
[province] => Wales
[location_detail] => SimpleXMLElement Object
(
)
[beds] => 0
[baths] => 0
[pool] => 0
=> SimpleXMLElement Object
(
)
[desc] => SimpleXMLElement Object
(
[en] =>
Lorem ipsum 2 dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat. Ut wisis enim ad minim veniam, quis nostrud exerci tution ullam corper suscipit lobortis nisi ut aliquip ex ea commodo consequat. Duis te feugi facilisi. Duis autem dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit au gue duis dolore te feugat nulla facilisi.
)
)
)
The structure of the array can be seen above but I cannot seem to be able to “get at” the values to add to a query.
I have been at this for a while so the obvious is doubtless staring me in the face.
TIO
C