<?php
class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate
{
private $xml;
public function __construct($filePath, $isUrl = true)
{
$this->isUrl = $isUrl;
$this->xml = new SimpleXmlIterator($filePath, 0, $isUrl);
}
public function offsetExists($offset)
{
return !is_null($this->xml->$offset);
}
public function offsetGet($offset)
{
if ($this->xml->getName() == $offset) {
return $this;
}
if (preg_match('/(\w+)\[(\d+)\]/', $offset, $nodes)) {
var_dump($this->xml->$nodes[1][$nodes[2]]->asXML());
return new self($this->xml->$nodes[1][$nodes[2]]->asXML(), false);
}
return new self($this->xml->$offset->asXML(), false);
}
public function __toString()
{
return (string) $this->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;
}
}
$str = '<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
<recursive>
<val>2</val>
<val>4</val>
</recursive>
</note>';
$xml = new XmlArrayAccessIterator($str, false);
//echo $xml['note']['to'];
$xml['note']['recursive']['val[1]'];
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/l4vfs on line 13
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/l4vfs on line 18
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/l4vfs on line 37
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/l4vfs on line 42
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/l4vfs on line 47
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/l4vfs on line 9
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/l4vfs on line 9
Warning: Array to string conversion in /in/l4vfs on line 25
Warning: Trying to access array offset on null in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Output for 8.2.0 - 8.2.18
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/l4vfs on line 13
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/l4vfs on line 18
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/l4vfs on line 37
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/l4vfs on line 42
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/l4vfs on line 47
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/l4vfs on line 9
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/l4vfs on line 9
Warning: Array to string conversion in /in/l4vfs on line 25
Warning: Trying to access array offset on value of type null in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Output for 8.1.0 - 8.1.28
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/l4vfs on line 13
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/l4vfs on line 18
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/l4vfs on line 37
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/l4vfs on line 42
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/l4vfs on line 47
Warning: Array to string conversion in /in/l4vfs on line 25
Warning: Trying to access array offset on value of type null in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Warning: Array to string conversion in /in/l4vfs on line 25
Warning: Trying to access array offset on value of type null in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Notice: Array to string conversion in /in/l4vfs on line 25
Notice: Trying to access array offset on value of type null in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
Notice: Array to string conversion in /in/l4vfs on line 25
Fatal error: Uncaught Error: Call to a member function asXML() on null in /in/l4vfs:25
Stack trace:
#0 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#1 {main}
thrown in /in/l4vfs on line 25
Process exited with code 255.
bool(false)
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/l4vfs:10
Stack trace:
#0 /in/l4vfs(10): SimpleXMLElement->__construct('', 0, false)
#1 /in/l4vfs(26): XmlArrayAccessIterator->__construct(false, false)
#2 /in/l4vfs(72): XmlArrayAccessIterator->offsetGet('val[1]')
#3 {main}
thrown in /in/l4vfs on line 10
Process exited with code 255.
Output for 5.1.2
Fatal error: Call to undefined method SimpleXMLIterator::getName() in /in/l4vfs on line 20
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.1
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/l4vfs:10
Stack trace:
#0 /in/l4vfs(10): SimpleXMLElement->__construct('<?xml version="...', 0, false)
#1 /in/l4vfs(68): XmlArrayAccessIterator->__construct('<?xml version="...', false)
#2 {main}
thrown in /in/l4vfs on line 10
Process exited with code 255.
Output for 5.0.3 - 5.0.4
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/l4vfs:10
Stack trace:
#0 /in/l4vfs(10): SimpleXMLIterator->__construct('<?xml version="...', 0, false)
#1 /in/l4vfs(68): XmlArrayAccessIterator->__construct('<?xml version="...', false)
#2 {main}
thrown in /in/l4vfs on line 10
Process exited with code 255.
Output for 5.0.0 - 5.0.2
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects exactly 1 parameter, 3 given' in /in/l4vfs:10
Stack trace:
#0 /in/l4vfs(68): XmlArrayAccessIterator->__construct('<?xml version="...', 0, false)
#1 {main}
thrown in /in/l4vfs on line 10
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/l4vfs on line 3
Process exited with code 255.