XML
|
|
|
|
<?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
|
|
|
|