Zend - The PHP Company




XML

Add Code


mxml  

Type: class
Added by: h3
Entered: 12/12/2003
Last modified: 02/11/2002
Rating: - (fewer than 3 votes)
Views: 5440
This is a minimal, and hopefully simple class to get data out of a xml file via path query. Like; /rootNode/subnode/node .. if you don't understand read the code source, you'll dig it :) It was inspired from lots of great codes from the open source (mainly from zend.com and pear.php.net), wich are not necessarly related to xml, list them back would be too long and I would surely forget some, so i felt like I had to give it back to the open source. now make something better out of it :) to do: - add support for duplicate nodes. - add support fo namespaces. note to coders: - the $i & $o stands for input & output, i think it's a clean and efficient way to handle mainstream data trough functions. - this code may not be appropriate for proffessional use since it does not meet the industry standards and is still in beta developement - I think it can handle malformed or broken xml files without ending with infinites loops or crashing, but you shall respect some rules to in your xml file composition .. and avoid node repetition :/ i'll work on that soon. ** BOF file.xml ** data data ** EOF file.xml ** finally, feel free to report me bugs, suggestions or comments. - h3 at mindkind dot org


<?php
// +-----------------------------------------------------------------------+
// | Copyright �2003-2004  h3                    Http://h3.valleyfield.net |
// | All   rights   reserved.                    E  x  p a  n  d  the  web |
// |                                                                       |
// | o Client  : open source release [ @ zend dot com]                     |
// | o File    : mxml.class.php                                            |
// | o Version : 1.0 beta                                                  |
// |                                                                       |
// | THIS SOFTWARE IS PROVIDED BY THE  COPYRIGHT HOLDERS  AND CONTRIBUTORS |
// | "AS IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE  COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES (INCLUDING,  BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,  OR  TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  SUCH DAMAGE. |
// |                                                                       |
// +-----------------------------------------------------------------------+
// | Author: h3 <h3@valleyfield.net , h3@mindkind.org>                     |
// +-----------------------------------------------------------------------+

class mxml {
    function 
mxml($i) {
        
$this->xfile file($i);
    }
    
# Get tag data (<node>data</node>) as String
    
function get_data($i) {
        
$xpath explode('/',$i);
        
$z     0;
        for(
$x=0;$x<=count($this->xfile);$x++) {
            if (
trim($xpath[$z])==""$z++;
            if(
ereg($xpath[$z],$this->xfile[$x])) {
                if(
$z==count($xpath)-1) {
                    return 
$this->xtractelm($xpath[$z],$x);
                    break;
                } else {
                    
$z++;
                }
            }
        }
    }
    
# Get tag attributes ( <node attr1="A" attr2="B" /> ) as Array
    
function get_attr($i) {
        
$xpath explode('/',$i);
        
$z     0;
        for(
$x=0;$x<=count($this->xfile);$x++) {
            if (
trim($xpath[$z])==""$z++;
            if(
ereg($xpath[$z],$this->xfile[$x])) {
                if(
$z==count($xpath)-1) {
                    return 
$this->xtractattr($this->xfile[$x]);
                    break;
                } else {
                    
$z++;
                }
            }
        }
    }
    
# Attributes extraction
    
function xtractattr($i) {
        
$i explode(" ",$i);
        for(
$x=0;$x<count($i);$x++) {
            if(!
ereg("<|>",$i[$x]) && trim($i[$x])!="") {
                
$tmp explode("=",$i[$x]);
                
$k trim($tmp[0]);
                
$chr['>']  = "";
                
$chr['"']  = "";
                
$o[$k] = strtr($tmp[1],$chr);
            }
        }
        return 
$o;
    }
    
# Fill variable untill we meet the closing tag from get_data
    
function xtractelm($i,$pointer) {
        if(isset(
$this->xfile) && is_array($this->xfile)) {
            for(
$x=$pointer;$x<count($this->xfile);$x++) {
                
$o .= $this->xfile[$x];
                if(
ereg('</'.$i.'>',$this->xfile[$x])) {
                    return 
trim(strip_tags($o));
                    break;
                }
            }
        }
    }
}
?>


Usage Example


<?php
include('mxml.class.php');
$o = new mxml($file);                        # parse the file
$data $o->get_data(/root/node/subnode);    # get node data
$attributes $o->get_attr(/root/node);       # get node attributes
echo $data;                                   # echo node data
print_r($attributes);                         # echo node attributes
?>


Rate This Script





Search



This Category All Categories