Mam problem z napisaniem parsera xml jako oddzielna klase.
Mam taki plik xml:
[xml:1:c6898525f8]<?xml version="1.0" encoding="iso-8859-2"?>
<module name="news">
<news id="1">
<title>Test XML</title>
<author>radziel</author>
<email>radziel@xx.xx</email>
<www>http://www.xxx.com</www>
<text>Oto testowy XML</text>
</news>
</module>[/xml:1:c6898525f8]
I mniej więcej taki parser
[php:1:c6898525f8]class news
{
var $id;
var $title;
var $author;
var $email;
var $www;
var $text;
function one_news($id)
{
$this -> id = $id;
$this -> www = '';
}
function GetAuthor()
{
return $this -> author;
}
}
$int = 0;
$newsy = array();
$is_news = 0;
function tag_start($parser, $attr, $params)
{
global $act_tag, $int, $newsy, $is_news;
if($attr == 'NEWS' && $is_news == 1)
{
die('[ _E_ERROR_ ] _E_XML_ERROR_ <br>');
}
elseif($attr == 'NEWS' && $is_news == 0)
{
$newsy[$int] = new news($params['ID']);
$is_news = 1;
}
if($is_news == 1)
{
if($attr != 'PERSONALIA')
{
$act_tag = $attr;
}
else
{
$newsy[$int] -> author = $params['AUTHOR'];
}
}
}
function tag_text($parser, $text)
{
global $act_tag, $int, $newsy, $is_news;
if($is_news == 1)
{
switch($act_tag)
{
case 'TITLE': $newsy[$int] -> title .= $text; break;
case 'AUTHOR': $newsy[$int] -> author .= $text; break;
case 'WWW': $newsy[$int] -> www .= $text; break;
case 'EMAIL': $newsy[$int] -> email .= $text; break;
case 'TEXT': $newsy[$int] -> text .= $text; break;
}
}
}
function tag_end($parser, $attr)
{
global $act_tag, $int, $is_news;
if($attr == 'NEWS' && $is_news == 1)
{
$int++;
$is_news = 0;
}
}
$parser = xml_parser_create();
xml_set_element_handler($parser, 'tag_start', 'tag_end');
xml_set_character_data_handler($parser, 'tag_text');
if(!($fp = fopen('../data/news.xml', 'r')))
{
die('['.E_ERROR_.'] '.E_XML_ERROR_FILE_);
}
while($data = fread($fp, 4096))
{
if(!xml_parse($parser, $data, feof($fp)))
{
die(sprintf("[".E_ERROR_."] ".E_XML_ERROR_." ' %s ' ".O_IN_LINE_." %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}
}
xml_parser_free($parser);
foreach($newsy as $news)
{
echo('<table width=100% border=0>');
echo('<tr align=left><td colspan=2><font size=2><b>'.$news -> title.'</b></td></tr>');
echo('<tr align=left><td><font size=1>Info: '.$news -> author.' napisał</td></tr>');
echo('<tr><td colspan=2><font size=1>'.$news -> text.'</td></tr>');
echo('<tr align=left><td><font size=1><a href=#>[więcej]</a></td></tr>');
echo('</table>');
}
?>[/php:1:c6898525f8]
Niestety do kazdego pliku muszę tworzyc oddzielny parser... bo w kazdym sa inne tagi... i ten parser-klasa nie jest elastyczny... chcialbym np. aby on umieszczal w tablicy tag i co w nim jest np.
[xml:1:c6898525f8]<title>Test nowego eP Engine</title>[/xml:1:c6898525f8]
i mam:
[php:1:c6898525f8]<?php
echo("$xml->tag[title]");
?>[/php:1:c6898525f8]
i dostaje "Test XML"
Pomógłby mi ktoś coś tagiego napisać ? :?
Może moj sposob nie jest ergonomiczny... ale... jestem poczatkujacy w XML :?