Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Xml loader

  1. #1

    Xml loader

    ciao....ho questo codice:
    <?php
    class XmlLoader {
    public $doc = null;
    public function __construct() {
    $doc = new DOMDocument();
    }
    public function loadXmlFile($fileName) {
    if(!$doc->load($fileName)) {
    throw new Exception('Error loading file ['.$fileName.']');
    }
    return new XmlParser($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();
    }
    ?>

    mi da errore nella riga: if(!$doc->load($fileName))

    dicendomi:

    Undefined variable: doc in C:\www\html\index.php on line 112

    Fatal error: Call to a member function load() on a non-object in C:\www\html\index.php on line 112

    qualcuno sa dirmi dov'è l'errore?

  2. #2
    io nel costruttore questa riga:
    $doc = new DOMDocument();
    la sostiotuirei con
    $this->doc = new DOMDocument();
    Dolcissima è la vita nella totale assenza di senno.

  3. #3
    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();
    }
    ?>
    Questo funziona senza problemi !
    L'errore nel tuo codice (codice postato qc tempo fà da me
    nella versione corretta ) è qui
    $doc->load($fileName
    che invece è
    $this->doc->load($fileName)
    si c'è anche qui lo stesso errore
    $doc = new DOMDocument();
    vedi post che segue
    questa si chiama aggregazione parlando in OOP.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4
    si il codice era il tuo , ti ricorderai sicuramente che sono alle primissime armi,ma si vede che provando a lavorarci su per capirlo ho dimenticato qualcosa :master: ....

    quindi quell' $this->doc..... è necessario?(stavo leggendo qualcosa che dove si diceva che è una aggrecazione Part of...è questo che intendevi?)

    cmq grazie adesso l'ho provato e va....

  5. #5
    object interation sono 4 tutorials + 2 per la composition.

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.