<?php
class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate
{
private $xml;
public function __construct(SimpleXmlIterator $xml)
{
$this->xml = $xml;
}
public function offsetExists($offset)
{
return !is_null($this->xml->$offset);
}
public function offsetGet($offset)
{
if ($this->xml->getName() == $offset) {
return $this;
}
$xml = $this->xml->$offset;
if (false === $xml->asXML()) {
$xml = $this->xml[$offset];
if (is_null($xml)) {
throw new RuntimeException('unknown node or attribute "' . $offset . '"');
}
}
return new self($xml);
}
public function offsetSet($offset, $value)
{
throw new RuntimeException('This is readonly. writing to "' . (string) $offset . '" with value "' . (string) $value . '" not possible');
}
public function offsetUnset($offset)
{
throw new RuntimeException('This is readonly. Unsetting "' . (string) $offset . '" not possible');
}
public function getIterator()
{
return $this->xml;
}
public function __toString()
{
return (string) $this->xml;
}
}
$xml = '<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading foo="bar">Reminder</heading>
<body>Don\'t forget me this weekend!</body>
<recursive>
<val>2</val>
<val>4</val>
</recursive>
</note>';
$simple = new SimpleXmlIterator($xml);
$xml = new XmlArrayAccessIterator($simple);
echo $xml['note']['to'];
echo $xml['note']['recursive']['val'][1];
echo $xml['note']['heading']['@foo'];
Deprecated: Return type of XmlArrayAccessIterator::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v23um on line 12
Deprecated: Return type of XmlArrayAccessIterator::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v23um on line 17
Deprecated: Return type of XmlArrayAccessIterator::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v23um on line 35
Deprecated: Return type of XmlArrayAccessIterator::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v23um on line 40
Deprecated: Return type of XmlArrayAccessIterator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/v23um on line 45
Tove4
Fatal error: Uncaught RuntimeException: unknown node or attribute "@foo" in /in/v23um:28
Stack trace:
#0 /in/v23um(77): XmlArrayAccessIterator->offsetGet('@foo')
#1 {main}
thrown in /in/v23um on line 28
Process exited with code 255.
Tove4
Fatal error: Uncaught RuntimeException: unknown node or attribute "@foo" in /in/v23um:28
Stack trace:
#0 /in/v23um(77): XmlArrayAccessIterator->offsetGet('@foo')
#1 {main}
thrown in /in/v23um on line 28
Process exited with code 255.
Tove4
Fatal error: Uncaught exception 'RuntimeException' with message 'unknown node or attribute "@foo"' in /in/v23um:28
Stack trace:
#0 /in/v23um(77): XmlArrayAccessIterator->offsetGet('@foo')
#1 {main}
thrown in /in/v23um on line 28
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.2
Fatal error: Call to undefined method SimpleXMLIterator::getName() in /in/v23um on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/v23um on line 3
Process exited with code 255.