#phpc on Freenode had a discussion tonight about how there’s nothing in XML that’s quite as simple as JSON for encoding types. I came up with this off-the-cuff – as opposed to WDDX, this is more JSON-like in that each ‘XML Document’ encodes only a single value – but that value might be mapped to any of the built-in types.
Of course, the <object> stuff would only work with pretty simple objects – but the fact that it tries at all to instance a class is a step beyond what JSON does, which is ALWAYS an instance of stdClass. You could drop this extra ‘feature’ by remove removing line 43 ($class = …) and changing line 44 to:
[highlight lang=php]$data = new stdClass;[/highlight]
[highlight lang=php]<?php
$xmlstr[] = <<
XMLSTR;
$xmlstr[] = <<
XMLSTR;
$xmlstr[] = <<
XMLSTR;
function parse($xmlstr) {
$xml = XMLReader::XML;
while($xml->read()) {
switch($xml->nodeType) {
case XMLReader::ELEMENT:
switch($xml->name) {
case ‘data’: continue;
case ‘int’: case ‘string’: case ‘bool’: case ‘double’:
return $xml->getAttribute(‘value’);
case ‘array’:
$in_array = true;
$data = array();
break;
case ‘object’:
$in_object = true;
$class = $xml->getAttribute(‘class’);
$data = new $class;
break;
case ‘entry’:
if ($in_array) {
$data[$xml->getAttribute(‘key’)] = $xml->getAttribute(‘value’);
}
break;
case ‘property’:
if ($in_object) {
$prop = $xml->getAttribute(‘name’);
$data->{$prop} = $xml->getAttribute(‘value’);
}
break;
default: /* What else is there? */
return null;
}
break;
case XMLReader::END_ELEMENT:
if ($xml->name == ‘data’) return $data;
break;
default: break;
}
}
return $data;
}
foreach($xmlstr as $x) var_dump(parse($x));
[/highlight]
XMLReader punts on clone
PHP5’s XMLR eader – which I used for my JSON -Like XML post, doesn’t implement a ‘clone’ handler, which came as quite a shock when I attempted to implement a wrapper class that would allow me to push values back into the strea
PHP5’s XMLR eader – which I used for my JSON -Like XML post, doesn’t implement a ‘clone’ handler, which came as quite a shock when I attempted to implement a wrapper class that would allow me to push values back into the strea
Tracked: Aug 12, 23:45