View Single Post
Old 07-17-2007, 02:47 PM   · #1
Barrucadu
Formally Mikor.
 
Barrucadu's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,546
NP$: 171.25 (Donate)
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
Simple XML Class

Here is a simple class to parse XML files:

PHP Code:
// Known Bugs:
//    Does not support nesting of complex types eg:
//        <tag><tag2><tag3>hi</tag3></tag2></tag>
//        would display as tag => null, tag2 => tag3 = "hi"
class parseXML{
    
// File URL
    
var $file;
    
    
//Private variables
    
var $parser;
    var
$data;
    var
$index_array;
    var
$data_array;
    var
$xml;
    
    
//Tag variables
    
var $root_tags = array();
    var
$sub_tags = array();
    
    function
presetRSS(){
        
// Set up the tags for RSS 2.0
        
$this->root_tags = array('title', 'link', 'description', 'language', 'lastBuildDate', 'copyright');
        
$this->sub_tags = array('item' => array('title', 'description', 'link', 'guid', 'pubDate', 'category'));
    }
    
    function
print_error(){
        
// Display xml error message
        
die(sprintf("XML Error: %s at line %d",
            
xml_error_string(xml_get_error_code($this->parser)),
            
xml_get_current_line_number($this->parser)
        ));
    }

    function
parse_file(){
        
// Create the parser
        
$this->parser = xml_parser_create();
        
// Skip whitespace between elements
        
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
        
// Disable upper-casing.
        
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
        
// Read the data into $data
        
$this->data = file_get_contents($this->file);
        
//parse XML input $data into two arrays:
        // $index_array - pointers to the locations of appropriate values in $data_array - data value array.
        
xml_parse_into_struct($this->parser, $this->data, $this->data_array, $this->index_array) or $this->print_error();
        
        
// Stick it all in a nice array
        
$this->xml = array();
        
        
// Handle root tags (format: $xml[tag])
        
foreach($this->root_tags as $tag){
            if(
count($this->data_array[$this->index_array[$tag][0]]['xml:attributes']) != 0){
                foreach(
$this->data_array[$this->index_array[$tag][0]]['xml:attributes'] as $akey => $avar){
                    
$this->xml[$tag]['attributes'][$akey] = $avar;
                }
            }
            
$this->xml[$tag] = $this->data_array[$this->index_array[$tag][0]]['value'];
        }
        
        
// Now for child tags (format: $xml[parent][number][child])
        
foreach($this->sub_tags as $key => $value){
            
$children = count($this->index_array[$key]) / 2;
            
$i = 0;
            
$j = 0;
            while (
$i < $children){
                
// Skip every 2nd one, as it is a closing tag and thus has no attributes
                
if($j % 2 == 0 || $j == 0){
                    if(
count($this->data_array[$this->index_array[$key][$j]]['attributes']) != 0){
                        foreach(
$this->data_array[$this->index_array[$key][$j]]['attributes'] as $akey => $avar){
                            
$this->xml[$key][$i]['xml:attributes'][$akey] = $avar;
                        }
                    }
                    foreach(
$value as $subtag){
                        if(
count($this->data_array[$this->index_array[$subtag][$i]]['attributes']) != 0){
                            foreach(
$this->data_array[$this->index_array[$subtag][$i]]['attributes'] as $akey => $avar){
                                
$this->xml[$key][$i][$subtag]['xml:attributes'][$akey] = $avar;
                            }
                        }
                        if(
is_array($this->sub_tags[$subtag])){
                            
$this->xml[$key][$i][$subtag] = 'xml:complex_tag';
                        }else{
                            
$this->xml[$key][$i][$subtag] = $this->data_array[$this->index_array[$subtag][$i]]['value'];
                        }
                    }
                    
$i ++;
                }
                
$j ++;
            }
        }

        
//unseting XML parser object
        
xml_parser_free($this->parser);
    }
}


Here is an example for parsing RSS 2.0 files (as you can see, there is a function to set the class up for parsing RSS)

PHP Code:
$xml = new parseXML;

// RSS 2.0 File (only tested with bbc news)
$xml->file = 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml';
$xml->presetRSS();

$xml->parse_file();
$parsedXML = $xml->xml;

echo
'<pre>';
print_r($parsedXML);
echo
'</pre>';


(I can't believe i'm on holiday in Poland, and still doing this)


Please register or log-in into NamePros to hide ads
__________________
Me | Last.fm | F@h | Archlinux.co.uk

archlinux User
Barrucadu is offline   Reply With Quote
Site Sponsors
Traffic Down Under Traffic Down Under Hunting Moon
Advertise your business at NamePros
All times are GMT -7. The time now is 02:28 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.