3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xpath = "/bids_offers/market_submit/sem_gen_technical_offer/block_deloading/point[2]"; $xml = new EformDOMDocument(); $root = $xml->createElement("bids_offers"); $xml->appendChild($root); $xml->buildMissingTree($xpath); $xml->formatOutput = true; echo $xml->saveXML(); class EformDOMDocument extends DOMDocument { public $xpath; public function __construct($version = null, $encoding = null) { parent::__construct($version, $encoding); $this->xpath = new DOMXPath($this); } public function setAttributeX($path, $attributeName, $attributeValue) { // check if path returns anything valid if ($this->xpath->query($path)->length == 0) { $this->buildMissingTree($path); } $this->xpath->query($path)->item(0)->setAttribute($attributeName, $attributeValue); } public function buildMissingTree($path) { $missingNodes = array(); $tempPath = $path; // build an array of missing elements in the path while ($this->xpath->query($tempPath)->length == 0) { $tempName = basename($tempPath); $tempPath = dirname($tempPath); // deal with such: testnode[@type='HOT'] or [#] if (strpos($tempName, '[') === FALSE) { // no attributes use $tempName $missingNodes[] = $this->createElement($tempName); } else { // split element name and attribute, create both separate, append attribute to element, push into the aray $tempHolder = explode('[', $tempName); $tempElement = $this->createElement($tempHolder[0]); // [1] is attribute, clean it up, split name and value if (strpos($tempHolder[1], '@') !== FALSE) { // element has attiribute if it has @ in the path piece $attParam = str_replace(array('@', '\'', ']'), '', $tempHolder[1]); $attribute = explode('=', $attParam); $tempElement->setAttribute($attribute[0], $attribute[1]); } // otherwise its a number of the node in the sequence, skip it, as if xml is generated is in order, //previous nodes should exist already $missingNodes[] = $tempElement; $tempElement = null; } } // build the structure with missing elements (use $missingNodes array) // get existing end echo $tempPath; var_dump($missingNodes[0]); $currentElement = $this->xpath->query($tempPath)->item(0); while (($newElement = array_pop($missingNodes) !== NULL)) { $currentElement->appendChild($newElement); $currentElement = $newElement; } } /** * Checks if document has at least one element with a name (and optional attribute) provided * * @param string $name * @return mixed */ public function hasElement($name, $attribute = '') { $elements = $this->getElementsByTagName($name); if ($elements->length > 0) { if ($attribute == '') { return true; } // if attribute provided, check through the list for the element with provided attribute for ($i = 0; $i < $elements->length; $i++) { // TODO fix that checks on attibute value too, not just name if ($elements->item($i)->hasAttribute($attribute)) { return $elements->item($i); //return true; } } } return false; } /** * * @param string $name * @param string $attributeParameters provided as "attr1name=value1;attr2name=value2...etc." * @return DOMElement */ public function createElementWithAttributes($name, $attributeParameters = '') { $element = $this->createElement($name); // atributes provided as "attr1name=value1;attr2name=value2...etc." if ($attributeParameters != '') { // separate multiple parameters $attributes = explode(';', $attributeParameters); // add parameters to the element foreach ($attributes as $attribute) { $keyvalue = explode('=', $attribute); $element->setAttribute($keyvalue[0], $keyvalue[1]); } } return $element; } }
Output for 8.3.0 - 8.3.6
Deprecated: DOMDocument::__construct(): Passing null to parameter #1 ($version) of type string is deprecated in /in/7p58O on line 15 Deprecated: DOMDocument::__construct(): Passing null to parameter #2 ($encoding) of type string is deprecated in /in/7p58O on line 15 /bids_offersobject(DOMElement)#4 (27) { ["schemaTypeInfo"]=> NULL ["tagName"]=> string(5) "point" ["className"]=> string(0) "" ["id"]=> string(0) "" ["firstElementChild"]=> NULL ["lastElementChild"]=> NULL ["childElementCount"]=> int(0) ["previousElementSibling"]=> NULL ["nextElementSibling"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["parentElement"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["isConnected"]=> bool(false) ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: DOMNode::appendChild(): Argument #1 ($node) must be of type DOMNode, true given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Deprecated: DOMDocument::__construct(): Passing null to parameter #1 ($version) of type string is deprecated in /in/7p58O on line 15 Deprecated: DOMDocument::__construct(): Passing null to parameter #2 ($encoding) of type string is deprecated in /in/7p58O on line 15 /bids_offersobject(DOMElement)#4 (23) { ["schemaTypeInfo"]=> NULL ["tagName"]=> string(5) "point" ["firstElementChild"]=> NULL ["lastElementChild"]=> NULL ["childElementCount"]=> int(0) ["previousElementSibling"]=> NULL ["nextElementSibling"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: DOMNode::appendChild(): Argument #1 ($node) must be of type DOMNode, bool given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 8.0.0 - 8.0.30
/bids_offersobject(DOMElement)#4 (23) { ["tagName"]=> string(5) "point" ["schemaTypeInfo"]=> NULL ["firstElementChild"]=> NULL ["lastElementChild"]=> NULL ["childElementCount"]=> int(0) ["previousElementSibling"]=> NULL ["nextElementSibling"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: DOMNode::appendChild(): Argument #1 ($node) must be of type DOMNode, bool given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
/bids_offersobject(DOMElement)#4 (18) { ["tagName"]=> string(5) "point" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, bool given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 7.0.16 - 7.0.33, 7.1.2 - 7.1.33, 7.2.0 - 7.2.34
/bids_offersobject(DOMElement)#4 (18) { ["tagName"]=> string(5) "point" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, boolean given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 7.0.0 - 7.0.15, 7.1.0 - 7.1.1
/bids_offersobject(DOMElement)#4 (17) { ["tagName"]=> string(5) "point" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, boolean given in /in/7p58O:63 Stack trace: #0 /in/7p58O(63): DOMNode->appendChild(true) #1 /in/7p58O(6): EformDOMDocument->buildMissingTree('/bids_offers/ma...') #2 {main} thrown in /in/7p58O on line 63
Process exited with code 255.
Output for 5.3.11 - 5.3.29, 5.4.1 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
/bids_offersobject(DOMElement)#4 (17) { ["tagName"]=> string(5) "point" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(5) "point" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(5) "point" ["baseURI"]=> NULL ["textContent"]=> string(0) "" } Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, boolean given, called in /in/7p58O on line 6 and defined in /in/7p58O on line 63
Process exited with code 255.
Output for 5.2.6 - 5.2.17, 5.3.0 - 5.3.10, 5.4.0
/bids_offersobject(DOMElement)#4 (0) { } Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, boolean given, called in /in/7p58O on line 6 and defined in /in/7p58O on line 63
Process exited with code 255.
Output for 5.2.3 - 5.2.5
/bids_offersobject(DOMElement)#4 (0) { } Warning: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in /in/7p58O on line 63 Fatal error: Call to a member function appendChild() on a non-object in /in/7p58O on line 63
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
/bids_offersobject(DOMElement)#4 (0) { } <br /> <b>Warning</b>: DOMNode::appendChild() expects parameter 1 to be DOMNode, boolean given in <b>/in/7p58O</b> on line <b>63</b><br /> <br /> <b>Fatal error</b>: Call to a member function appendChild() on a non-object in <b>/in/7p58O</b> on line <b>63</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.0.4
/bids_offersobject(DOMElement)#4 (0) { } <br /> <b>Warning</b>: appendChild() expects parameter 1 to be DOMNode, boolean given in <b>/in/7p58O</b> on line <b>63</b><br /> <br /> <b>Fatal error</b>: Call to a member function appendChild() on a non-object in <b>/in/7p58O</b> on line <b>63</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 T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/7p58O</b> on line <b>12</b><br />
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
<br /> <b>Parse error</b>: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/7p58O</b> on line <b>12</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in <b>/in/7p58O</b> on line <b>12</b><br />
Process exited with code 255.

preferences:
360.23 ms | 401 KiB | 468 Q