Класс-наследник можно создавать универсальный (но обычно получаются монстры, которые медленно работают), а можно сделать класс под конкретную задачу - то есть под конкретный xml.
Вот, создаем класс-наследник. Переопределяем метод parse (родительский метод будет вызываться внутри).
Вверху в комментариях написан ожидаемый xml.
/*
<stationlist>
<st>
<n>/101erotic</n>
<t>101.ru: Erotic</t>
<d>Unspecified description</d>
<ct>audio/mpeg</ct>
<b>0</b>
<s>THORNTON, Phil - The Healing Circle 18:12</s>
<g>N/A</g>
<l>6</l>
</st>
<st>
...
</st>
</stationlist> */
class("BUFLICK::xml2ds","xmlreader")
{
function parse()
{
if ($0 == "")
{
debug "[xml2ds::parse] The name of the file to parse was not specified"
return $false
}
if (!$file.exists($0))
{
debug "[xml2ds::parse] The file to parse was not found"
return $false
}
if(!$$->$xmlreader::parse($file.read($0)))
{
debug "[xml2ds::parse] Critical error: $$->$xmlreader::lastError()"
return $false;
}
else
{
return @%stations
}
}
internal function onDocumentStart()
{
//debug "Document Start"
@%stations = $array();
@%currentindex = 0;
return $true;
}
internal function onDocumentEnd()
{
//debug "Document End"
return $true;
}
internal function onElementStart()
{
//debug <$0>
if ($0 == stationlist) return $true
if ($0 != st)
{
@%currentkey = $0
}
return $true;
}
internal function onElementEnd()
{
//debug </$0>
if ($0 == st) @%currentindex++
return $true;
}
function constructor()
{
}
internal function onWarning()
{
warning $0
return $true;
}
internal function onText()
{
//debug $0
%text = $0;
//Trim leading and trailing space characters
%text = $str.strip(%text)
if (%text)
{
if (@%currentkey == n)
{
%text =~ s/^\///g
}
//debug @%currentindex : \"%text\"
@%stations[@%currentindex]{@%currentkey} = %text
}
return $true;
}
}
Использование:
%xml = $new(BUFLICK::xml2ds);
%stationsArr = %xml->$parse("C:\\buflick_temp_l.xml")
if (!%stationsArr)
{
debug "[BUFLICK::parseXMl] Error while parsing xml-file $0"
return
}
Для понимания, что происходит, можешь раскомментировать множественные debug операторы.