Jump to content

XML controller


tobeyt23

Recommended Posts

You're in luck :)

 

<?php
class XMLParser{
    var $parser;
    var $filePath;
    var $document;
    var $currTag;
    var $tagStack;
var $data;
var $state;
    function XMLParser($path,$state) {
        $this->parser = xml_parser_create();
    $this->filePath = $path;
    $this->document = array();
    $this->currTag =& $this->document;
    $this->tagStack = array();
$this->state = $state;
    }  
    function parse(){
        xml_set_object($this->parser, $this);
        xml_set_character_data_handler($this->parser, 'dataHandler');
        xml_set_element_handler($this->parser, 'startHandler', 'endHandler');
        

	if($this->state == 0) {
		if(!($fp = fopen($this->filePath, "r"))) {
			die("Cannot open XML data file: $this->filePath");
			return false;
		}
		while($data = fread($fp, 40960)) {
			if(!xml_parse($this->parser, $data, feof($fp))) {
				die(sprintf("XML error: %s at line %d",
							xml_error_string(xml_get_error_code($this->parser)),
							xml_get_current_line_number($this->parser)));
			}
			$this->data = $this->data.$data;
			fclose($fp);
		}
	} else {
			$data = $this->filePath;
			if(!xml_parse($this->parser, $data, false)) {
				die(sprintf("XML error: %s at line %d",
							xml_error_string(xml_get_error_code($this->parser)),
							xml_get_current_line_number($this->parser)));
			}			
	}
    
        
    xml_parser_free($this->parser);
    
        return true;
    }
    function startHandler($parser, $name, $attribs) {
        if(!isset($this->currTag[$name]))
            $this->currTag[$name] = array();
        
        $newTag = array();
        if(!empty($attribs))
            $newTag['attr'] = $attribs;
        array_push($this->currTag[$name], $newTag);
        
        $t =& $this->currTag[$name];
        $this->currTag =& $t[count($t)-1];
        array_push($this->tagStack, $name);
    }
    
    function dataHandler($parser, $data){
        $data = trim($data);
        
        if(!empty($data))
        {
            if(isset($this->currTag['data']))
                $this->currTag['data'] .= $data;
            else
                $this->currTag['data'] = $data;
        }
    }
    
    function endHandler($parser, $name)
    {
        $this->currTag =& $this->document;
        array_pop($this->tagStack);
        
        for($i = 0; $i < count($this->tagStack); $i++)
        {
            $t =& $this->currTag[$this->tagStack[$i]];
            $this->currTag =& $t[count($t)-1];
        }
    }
}
?>

 

Enjoy, heres how to use.

<?php
$result1 = $xml_class_name->document['OPENINGTAG'][0]['ROW'][$x]['attr']['ATTRIBUTE'];
//or data
$result2 = $xml_class_name->document['OPENINGTAG'][0]['ROW'][$x]['data'];
?>

 

<openingtag>

<row attribute="12">Some data</row>

<row attribute="13">Some more</row>

</openingtag>

 

Result1 = 12 assuming x is 0

Result2 = Some data assuming x is 0

Result1 = 13 assuming x is 1

Result2 = Some more assuming x is 1

Result1 & 2 = null assuming x is greater then 1

 

 

[EDIT]

However MySQL is a hell of alot quicker.

Link to comment
Share on other sites

However MySQL is a hell of alot quicker.

I'll second that. Using some slow parsed/tokenized/interpreted php code to scan through and find information in xml data from a file is going to be about 100 times slower than using a database engine, which uses complied code to do the scanning and finding of information. So, if you expect the amount of data to continually grow and want your web site to perform well, use a database.

Link to comment
Share on other sites

Although this deviates off the OP's question slightly, I thought this may be worth posting.

 

http://www.phpro.org/articles/Application-Configuration.html

 

It gives a comparison for storing application configuration information (not entire articles).

While MySQL is definitely going to be faster for large sets of data, it is not necessarily faster for small amounts of information.

Link to comment
Share on other sites

If its not faster for small amounts of data, you dont have it indexed correctly :)

 

the only time that reading a file will be faster really is when you are reading it per line, and not sorting in anway;

 

I've used both and do use both, XML is better as a ... bridge, an API between two systems, MySQL is faster in every instance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.