SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: XML help

  1. #1
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML help

    I am very new to the practical aspects of XML and PHP, although I do understand the basics of XML.

    I am looking to grab an XML file from the Internet and store its information in a database and I have completely no idea how to do that except for fopen() to grab the file.

    My goals:
    - Slurp in "ftp://ftp.ny.frb.org/xml/ForEx/Noon/latest.xml"
    - Stamp each country/rate into a MySQL database table

    As easy as this may sound, I cannot figure it out for the life of me. Please help. I am running PHP 4.3.3 if that helps any.

    Thx!

  2. #2
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Here's one way. Everything is done except the code to insert the rate into the database. Check out http://ca.php.net/manual/en/ref.xml.php for more information on how to deal with xml in php4.

    Luckily I wanted a good example to explore Dependancy Injection before I go to a presentation "Test Driven Development with Mock Objects and Dependency Injection -- What, Why and How" and this fit part of the bill.

    PHP Code:
    $xmlLocation 'ftp://ftp.ny.frb.org/xml/ForEx/Noon/latest.xml';
    $xmlParser = new XMLRateParser($xmlLocation);
    $rates $xmlParser->parse();
    foreach(
    $rates as $rate) {
         
    $rate->toString();
         
    $rate->save();
    }

    class 
    Rate {
        function 
    toString() {
            echo 
    '<pre>';
            
    print_r($this);
            echo 
    '</pre>';
        }
        function 
    save() {
            
    // write code to save this obj to the db.
            
    $sql '...';
            
        }
    }
    class 
    XMLParser {
        var 
    $xmlLocation;
        function 
    XMLParser($xmlLocation) {
            
    $this->xmlLocation $xmlLocation;
        }
        function 
    parse() {
            
    $parser xml_parser_create();
            
    xml_set_object($parser$this);
            
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
            
    xml_set_element_handler($parser'handleOpenTag''handleCloseTag');
            
    xml_set_character_data_handler($parser'handleCharacterData');
            
    xml_set_processing_instruction_handler($parser'handleProcessingInstructor');
            
    xml_set_default_handler($parser'handleDefault');
            
    $xml file_get_contents($this->xmlLocation);
            if (!(
    xml_parse($parser$xmltrue)) &&
            (
    xml_get_error_code($parser) != XML_ERROR_NONE)) {
                
    trigger_error('XML Error : ' .
                
    xml_error_string(xml_get_error_code($parser)) .
                
    ' in file ' $this->xmlLocation .
                
    ' at line ' xml_get_current_line_number($parser));
            }
            
    xml_parser_free($parser);
        }
        function 
    handleOpenTag($parser$name$attributes) {}
        function 
    handleCloseTag($parse$name) {}
        function 
    handleCharacterData($parser$data) {}
        function 
    handleProcessingInstructor($parser$target$data) {}
        function 
    handleDefault($parser$string) {}
    }
    class 
    XMLRateParser extends XMLParser {
        var 
    $current;
        var 
    $rate;
        var 
    $currentAttr;
        var 
    $result = array();
        function 
    parse() {
            
    parent::parse();
            return 
    $this->result;
        }
        function 
    handleOpenTag($parser$name$attributes) {
            
    $this->current $name;
            
    $this->currentAttr $attributes;
            if (
    $name == 'Rate') {
                
    $this->rate =& new Rate();
                
    $this->result[] =& $this->rate;
                
    $this->rate->time $attributes['TimeOfRate'];
                
    $this->rate->type $attributes['TypeOfRate'];
                
    $this->rate->id $attributes['Id'];
            }
        }
        function 
    handleCharacterData($parser$data) {
            if(
    trim($data) != '') {            
                switch(
    $this->current) {
                    case 
    'Date':
                    
    $this->rate->date $data;
                    break;
                    case 
    'Country':
                    
    $this->rate->country $data;
                    
    $this->rate->unit $this->currentAttr['UnitName'];
                    break;
                    case 
    'Value':
                    
    $this->rate->value $data;
                    
    $this->rate->usdollars $this->currentAttr['InUsd'];
                    break;
                }
            }
        }


  3. #3
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think I understand a damn thing in the code, but it does parse through the file quite nicely and allow me to easily access the information. Thanks for that!

    I will also thumb through that reference link... if you have any others please feel free to pass them on.

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    Calgary, Alberta, Canada
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trogdor1024
    I will also thumb through that reference link... if you have any others please feel free to pass them on.
    Sitepoint has a good article on parsing rss with php4 : http://www.sitepoint.com/article/php...arsing-rss-1-0

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not directly in relation to your thread, but without a doubt at one point you'll be using XSL-T so look towards www.xml.com (http://www.xml.com/pub/at/16) to begin with.

    Can also recommend XML in a Nutshell from www.oreilly.com. Chapter 9 deals with XPATH, which you can download btw.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •