<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?
// nejdřív definice třídy
class bilance
{
var $soubor;
var $bilance=0;
var $priv_jeprijem=false;
var $priv_znacka="";
var $priv_parser;
function PocatecniZnacka ($parser, $nazev, $atributy)
{
$this->priv_znacka=$nazev;
if ($this->priv_znacka=="PRIJEM") $this->priv_jeprijem=true;
if ($this->priv_znacka=="VYDAJ") $this->priv_jeprijem=false;
}
function KoncovaZnacka ($parser, $nazev) {}
function data ($parser, $data)
{
if ($this->priv_znacka=="CASTKA" and $this->priv_jeprijem) $this->bilance+=$data;
if ($this->priv_znacka=="CASTKA" and !$this->priv_jeprijem) $this->bilance-=$data;
}
function parse ()
{
$this->priv_parser=xml_parser_create();
xml_set_object($this->priv_parser, $this);
xml_set_element_handler ($this->priv_parser, "PocatecniZnacka", "KoncovaZnacka");
xml_set_character_data_handler($this->priv_parser, "data");
if (!($obsahsouboru = fopen($this->soubor, "r"))) die("Nemohu otevřít XML pro čtení.");
while ($data = fread($obsahsouboru, 1024)) xml_parse($this->priv_parser, $data, feof($obsahsouboru));
xml_parser_free($this->priv_parser);
}
}
// teď vlastní program
$moje_bilance = new bilance;
$moje_bilance->soubor = "./finance.xml";
$moje_bilance->parse();
echo $moje_bilance->bilance;
?>