PHP Classes
elePHPant
Icontem

File: include_xml.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of B.W. Kemps  >  XmlNode  >  include_xml.php  >  Download  
File: include_xml.php
Role: Class source
Content type: text/plain
Description: The actual file
Class: XmlNode
Parse XML documents into a tree of node objects
Author: By
Last change: added methods:
&findFirstChild(name) (returns the first child node with a specific name)
&findNextSibling(name)
findAllChildren(name) (returns an array of al child nodes w.a.s.n.)
Date: 8 years ago
Size: 4,711 bytes
 

Contents

Class file image Download
<?php

class XmlNode
{
    var
$name;
    var
$attrs;
    var
$parent;
    var
$nextSibling;
    var
$previousSibling;
    var
$children;
    var
$lastChild;
    var
$firstChild;
    var
$text;

    function
XmlNode()
    {
       
$this->children = array();
    }

    function &
addNewChildNode()
    {
        return
$this->addChildNode(new XmlNode);
    }

    function
getAttributeTree($attribute)
    {
       
$nodes=array();
       
$nodes[]=$this->attrs[$attribute];
       
$child =& $this->firstChild;
        while (!
is_null($child))
        {
           
$nodes[]=$child->getAttributeTree($attribute);
           
$child =& $child->nextSibling;
        }
        return
$nodes;
    }

    function
setAttributeTree($attribute, $nodes)
    {
       
$this->attrs[$attribute]=$nodes[0];
       
$teller=0;
        while (++
$teller<count($nodes))
        {
           
$this->children[$teller-1]->setAttributeTree($attribute, $nodes[$teller]);
        }
    }

    function &
addChildNode(&$node)
    {
       
$this -> children[] =& $node;
       
$node->parent =& $this;
        if (
$this->countChildren()>1)
        {
           
$this->lastChild->nextSibling =& $node;
           
$node->previousSibling =& $this->lastChild;
        }
        else
$this->firstChild =& $node;
       
$this->lastChild =& $node;
        return
$node;
    }

    function
countChildren()
    {
        return
count($this->children);
    }
   
    function
hasChildren()
    {
        return
count($this->children)>0;
    }

    function
hasParent()
    {
        return !
is_null($this->parent);
    }

    function
toTree($niveau,$beforeGroup,$beforeItem,$afterItem,$afterGroup)
    {
       
$tree = $beforeItem.$this->name;
        foreach (
$this->attrs as $a => $v) $tree.='('.$a.'='.$v.')';
        if (!
is_null($this->text)) $tree.= " -".$this->text."-";
       
$tree.=$afterItem."\n";
        if (
$this->hasChildren())
        {
           
$tree.=$beforeGroup;
            foreach (
$this->children as $child) $tree.=$child->toTree($niveau+1,$beforeGroup,$beforeItem,$afterItem,$afterGroup);
           
$tree.=$afterGroup;
        }
        while (
$niveau-->0) $tree=' '.$tree;
        return
$tree;
    }

    function
toHtmlTree()
    {
        return
"<UL>".$this->toTree(-999,"<UL>","<LI>","</LI>","</UL>")."</UL>";
    }

    function
toXml($niveau)
    {
       
$xml.= '<'.strtolower($this->name);
        foreach (
$this->attrs as $a => $v) $xml.=' '.strtolower($a).'="'.$v.'"';
       
$xml.= ">";
        if (
$this->hasChildren())
        {
           
$xml.="\n";
            foreach (
$this->children as $child) $xml.=$child->toXml($niveau+1);
            while (
$niveau-->0) $xml=' '.$xml.' ';
        }
        else
        {
            while (
$niveau-->0) $xml=' '.$xml;
           
$xml.=$this->text;
        }
        if (
$niveau==0) $xml="<?xml version='1.0' ?>\n".$xml;
        return
$xml.'</'.strtolower($this->name).">\n";
    }

    function &
findFirstChild($name)
    {
       
$i = -1;
        while (
is_null($node) && ++$i<$this->countChildren())
            if (
strtolower($this->children[$i]->name)==strtolower($name)) $node =& $this->children[$i];
        return
$node;
    }

    function &
findNextSibling($name)
    {
       
$next =& $this->nextSibling;
        while (
is_null($node) && !is_null($next))
        {
            if (
strtolower($next->name)==strtolower($name)) $node=& $next;
           
$next =& $next->nextSibling;
        }
        return
$node;
    }

    function &
findChildren($name)
    {
       
$children = array();
       
$i = -1;
        while (++
$i<$this->countChildren())
            if (
strtolower($this->children[$i]->name)==strtolower($name)) $children[] =& $this->children[$i];
        return
$children;
    }
}

function
XmlStartElement($parser, $name, $attrs)
{
    global
$XmlRootNode;
    global
$XmlParentId;
   
$XmlRootNode[] = new XmlNode;
   
$node =& $XmlRootNode[count($XmlRootNode)-1];
    if (
$XmlParentId>=0) $node->parentId = $XmlParentId;
   
$node->name = $name;
   
$node->attrs = $attrs;
   
$XmlParentId = count($XmlRootNode)-1;
}

function
XmlCharacterData($parser, $text)
{
    global
$XmlRootNode;
    global
$XmlParentId;
   
$text=trim(trim(trim($text,"\t"),"\n"));
    if (
strlen($text)>0) $XmlRootNode[$XmlParentId]->text=$text;
}

function
XmlEndElement($parser, $name)
{
    global
$XmlRootNode;
    global
$XmlParentId;
   
$XmlParentId = $XmlRootNode[$XmlParentId]->parentId;
}

function &
XmlReadFile($file)
{
    global
$XmlRootNode,$Xml_parser;
   
$XmlRootNode = array();
   
$Xml_parser = Xml_parser_create();
   
Xml_set_element_handler($Xml_parser, "XmlStartElement", "XmlEndElement");
   
Xml_set_character_data_handler($Xml_parser, "XmlCharacterData");
    if (!(
$fp = fopen($file, "r"))) die("could not open Xml input");
    while (
$data = fread($fp, 4096)) if (!Xml_parse($Xml_parser, $data, feof($fp)))
    die(
sprintf("Xml error: %s at line %d", Xml_error_string(Xml_get_error_code($Xml_parser)), Xml_get_current_line_number($Xml_parser)));
   
Xml_parser_free($Xml_parser);
   
$teller=0;while (++$teller<count($XmlRootNode)) $XmlRootNode[$XmlRootNode[$teller]->parentId]->addChildNode($XmlRootNode[$teller]);
    return
$XmlRootNode[0];
}

?>