xml_set_object

(PHP 4 , PHP 5)

xml_set_object -- Use XML Parser within an object

Description

void xml_set_object ( resource parser, object &object)

This function allows to use parser inside object. All callback functions could be set with xml_set_element_handler() etc and assumed to be methods of object.

Example 1. xml_set_object() example

<?php
class xml  {
    var
$parser;

    function
xml()
    {
        
$this->parser = xml_parser_create();

        
xml_set_object($this->parser, $this);
        
xml_set_element_handler($this->parser, "tag_open", "tag_close");
        
xml_set_character_data_handler($this->parser, "cdata");
    }

    function
parse($data)
    {
        
xml_parse($this->parser, $data);
    }

    function
tag_open($parser, $tag, $attributes)
    {
        
var_dump($parser, $tag, $attributes);
    }

    function
cdata($parser, $cdata)
    {
        
var_dump($parser, $cdata);
    }

    function
tag_close($parser, $tag)
    {
        
var_dump($parser, $tag);
    }

}
// end of class xml

$xml_parser = new xml();
$xml_parser->parse("<A ID='hallo'>PHP</A>");
?>

00BB">$aa as $k=>$v)
            
$this->$k = $aa[$k];
    }
}

function
readDatabase($filename)
{
    
// read the XML database of aminoacids
    
$data = implode("", file($filename));
    
$parser = xml_parser_create();
    
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    
xml_parse_into_struct($parser, $data, $values, $tags);
    
xml_parser_free($parser);

    
// loop through the structures
    
foreach ($tags as $key=>$val) {
        if (
$key == "molecule") {
            
$molranges = $val;
            
// each contiguous pair of array entries are the
            // lower and upper range for each molecule definition
            
for ($i=0; $i < count($molranges); $i+=2) {
                    
$offset = $molranges[$i] + 1;
                
$len = $molranges[$i + 1] - $offset;
                
$tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return
$tdb;
}

function
parseMol($mvalues)
{
    for (
$i=0; $i < count($mvalues); $i++) {
        
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
    }
    return new
AminoAcid($mol);
}

$db = readDatabase("moldb.xml");
echo
"** Database of AminoAcid objects:\n";
print_r($db);

?>