3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Class JsonSimpleXMLElementDecorator * * Implement JsonSerializable for SimpleXMLElement as a Decorator */ class JsonSimpleXMLElementDecorator implements JsonSerializable { /** * @var SimpleXMLElement */ private $subject; public function __construct(SimpleXMLElement $element) { $this->subject = $element; } /** * Specify data which should be serialized to JSON * * @return mixed data which can be serialized by json_encode. */ public function jsonSerialize() { $subject = $this->subject; $array = array(); // json encode attributes if any. if ($attributes = $subject->attributes()) { $array['@attributes'] = array_map('strval', iterator_to_array($attributes)); } // traverse into children if applicable $children = $subject; // json encode child elements if any. group on duplicate names as an array. foreach ($children as $name => $element) { $decorator = new self($element); if (isset($array[$name])) { if (!is_array($array[$name])) { $array[$name] = [$array[$name]]; } $array[$name][] = $decorator; } else { $array[$name] = $decorator; } } // json encode non-whitespace element simplexml text values. $text = trim($subject); if (strlen($text)) { if ($array) { $array['@text'] = $text; } else { $array = $text; } } // return empty elements as NULL (self-closing or empty tags) if (!$array) { $array = NULL; } return $array; } } $buffer = ' <?xml version="1.0"?> <root attribute="variable"> <sampleElement> Text <sampleChild /> <sampleChild /> </sampleElement> <sampleElement> test <sampleChild attribute="variable">text</sampleChild> </sampleElement> </root> '; $xml = new SimpleXMLElement($buffer); $xml = new JsonSimpleXMLElementDecorator($xml); echo json_encode($xml, JSON_PRETTY_PRINT), "\n";
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of JsonSimpleXMLElementDecorator::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/VelFu on line 24 Warning: SimpleXMLElement::__construct(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): <?xml version="1.0"?> in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): ^ in /in/VelFu on line 85 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/VelFu:85 Stack trace: #0 /in/VelFu(85): SimpleXMLElement->__construct('\n<?xml version=...') #1 {main} thrown in /in/VelFu on line 85
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Warning: SimpleXMLElement::__construct(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): <?xml version="1.0"?> in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): ^ in /in/VelFu on line 85 Fatal error: Uncaught Exception: String could not be parsed as XML in /in/VelFu:85 Stack trace: #0 /in/VelFu(85): SimpleXMLElement->__construct('\n<?xml version=...') #1 {main} thrown in /in/VelFu on line 85
Process exited with code 255.
Output for 5.4.27 - 5.4.45, 5.5.11 - 5.5.38, 5.6.0 - 5.6.40
Warning: SimpleXMLElement::__construct(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): <?xml version="1.0"?> in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): ^ in /in/VelFu on line 85 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/VelFu:85 Stack trace: #0 /in/VelFu(85): SimpleXMLElement->__construct('\n<?xml version=...') #1 {main} thrown in /in/VelFu on line 85
Process exited with code 255.
Output for 5.4.0 - 5.4.26, 5.5.0 - 5.5.10
Warning: SimpleXMLElement::__construct(): Entity: line 2: parser error : XML declaration allowed only at the start of the document in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): <?xml version="1.0"?> in /in/VelFu on line 85 Warning: SimpleXMLElement::__construct(): ^ in /in/VelFu on line 85 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /in/VelFu:85 Stack trace: #0 /in/VelFu(85): SimpleXMLElement->__construct('?<?xml version=...') #1 {main} thrown in /in/VelFu on line 85
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/VelFu on line 43
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/VelFu on line 43
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/VelFu on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/VelFu on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/VelFu on line 8
Process exited with code 255.

preferences:
296.73 ms | 401 KiB | 459 Q