* Thus wrote Yoed Anis:
> Hi guys,
>
> OK I need some ideas.
>
> Somebody created the stupidest XML file I've ever seen. And of
> course they can't change it, and I *must* be able to read it. I'm all out of
> brain power on thinking how to go about reading it. I typically use
> simplexml to read xml and that's where my knowledge end.
>
> Heres the problem:
>
> <Catalog>
> <Rate>
> <RateCode>1</RateCode>
> <RateCurrency>USD<RateCurrency>
> <RateValue>123</RateValue>
> </Rate>
> <RateDescription>
> <Desc>This is dumn</Desc>
> </RateDescription>
I've seen worse :)
You'll have to set up a class that can keep track of the state of
your xml file:
class StupidCatalog {
var $code = 0;
var $current = 'USD';
var $value = '';
var $description = array();
}
class StupidCatalogParser {
var $catalog; // Collection of rates
var $current_catalog; // building this one
var $state; // tag we're working on
function startElement($parser, $name, $attr) {
$this->state = $name;
switch($name) {
case 'Rate':
// start working on a new
$this->current_catalog = new StupidCatalog();
break;
//...
}
}
function endElement($parser, $name) {
if($name == 'Rate') {
// reference is important.
$this->catalog[] = &$this->current_catalog;
}
}
function elementData($parser, $data) {
switch ($this->state) {
case 'RateDescription':
// tricky...
$this->current_catalog->{$this->state}[] = $data;
break;
case 'RateCode': // passthrough
case 'RateValue': // and the rest...
// more tricky...
$this->current_cataglog->{$this->state} = $data;
break;
}
}
Just create a new StupidCatalogParser() and assign the methods to
the appropriate call backs in http://php.net/xml
btw, the above is untested and only theory :)
HTH,
Curt
--
The above comments may offend you. flame at will.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php