Ciao.
Codice PHP:
<?php
class XmlLoader {
public $doc = null;
public function __construct() {
$this->doc = new DOMDocument();
}
public function loadXmlFile($fileName) {
if(!$this->doc->load($fileName)) {
throw new Exception('Error loading file ['.$fileName.']');
}
return new XmlParser($this->doc);
}
}
class XmlParser
{
public $doc = null;
public function __construct($doc) {
$this->doc = $doc;
}
public function save($fileName) {
if(!$this->doc->save($fileName)){
throw new Exception('Error saving file ['.$fileName.']');
}
}
//rss
public function getDocumentElement()
{
return $this->doc->documentElement;
}
//item
public function getItems()
{
return $this->getDocumentElement()->getElementsByTagName("item");
}
}//
try{
$doc = new XmlLoader();
$docXmlParser = $doc->loadXmlFile('http://www.corriere.it/rss/homepage.xml');
//get all items returns a DomNodeList
$elements = $docXmlParser->getItems();
foreach($elements as $node) {
print $node->textContent . "\n";
}
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
Per esempio.