<?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;
}
return new self($this->xml->$offset);
}
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($filePath);
$xml = new XmlArrayAccessIterator($simple);
echo $xml['note']['to'][0];
echo $xml['note']['recursive']['val'][0];
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/IYrra 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/IYrra 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/IYrra on line 26
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/IYrra on line 31
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/IYrra on line 36
Warning: Undefined variable $filePath in /in/IYrra on line 62
Deprecated: SimpleXMLElement::__construct(): Passing null to parameter #1 ($data) of type string is deprecated in /in/IYrra on line 62
Fatal error: Uncaught Exception: String could not be parsed as XML in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Warning: Undefined variable $filePath in /in/IYrra on line 62
Fatal error: Uncaught Exception: String could not be parsed as XML in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Notice: Undefined variable: filePath in /in/IYrra on line 62
Fatal error: Uncaught Exception: String could not be parsed as XML in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.26
Fatal error: Uncaught Exception: String could not be parsed as XML in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28
Notice: Undefined variable: filePath in /in/IYrra on line 62
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in /in/IYrra on line 62
Process exited with code 255.
Output for 5.1.2 - 5.1.6, 5.2.0 - 5.2.2
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in <b>/in/IYrra</b> on line <b>62</b><br />
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.1
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLElement->__construct('')
#1 {main}
thrown in <b>/in/IYrra</b> on line <b>62</b><br />
Process exited with code 255.
Output for 5.0.3 - 5.0.4
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 /in/IYrra(62): SimpleXMLIterator->__construct('')
#1 {main}
thrown in <b>/in/IYrra</b> on line <b>62</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.0.2
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/IYrra:62
Stack trace:
#0 {main}
thrown in <b>/in/IYrra</b> on line <b>62</b><br />
Process exited with code 255.
Output for 4.4.2 - 4.4.9
<br />
<b>Parse error</b>: syntax error, unexpected T_STRING, expecting '{' in <b>/in/IYrra</b> on line <b>3</b><br />
Process exited with code 255.