3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //$xpath = "/x:bids_offers/x:market_submit/x:sem_gen_technical_offer/x:block_deloading/x:point"; $xpath = "//bids_offers/market_submit/sem_gen_technical_offer/block_deloading/point"; $xml = new EformDOMDocument("1.0", "UTF-8"); //$root = $xml->createElement("bids_offers"); //$xml->appendChild($root); //$root->appendChild($xml->createElement("market_submit")); //$xml->formatOutput = true; //echo $xml->saveXML(); $str = <<<XML <?xml version="1.0" encoding="UTF-8"?> <bids_offers> <market_submit> <sem_gen_technical_offer> </sem_gen_technical_offer> </market_submit> </bids_offers> XML; $xml->loadXML($str); //$rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI); //$xml->xpath->registerNamespace('x', $rootNamespace); $xml->buildMissingTree($xpath); //$xml->formatOutput = true; $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 7.0.0 - 7.1.0
Fatal error: Uncaught DOMException: Wrong Document Error in /in/f1lGb:86 Stack trace: #0 /in/f1lGb(86): DOMNode->appendChild(Object(DOMElement)) #1 /in/f1lGb(28): EformDOMDocument->buildMissingTree('//bids_offers/m...') #2 {main} thrown in /in/f1lGb on line 86
Process exited with code 255.
Output for 5.0.0, hhvm-3.10.0 - 3.13.2

Process exited with code 139.
Output for 5.0.5 - 5.1.1, 5.1.3 - 5.6.28
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in /in/f1lGb:86 Stack trace: #0 /in/f1lGb(86): DOMNode->appendChild(Object(DOMElement)) #1 /in/f1lGb(28): EformDOMDocument->buildMissingTree('//bids_offers/m...') #2 {main} thrown in /in/f1lGb on line 86
Process exited with code 255.
Output for 5.1.2
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in /in/f1lGb:87 Stack trace: #0 /in/f1lGb(87): DOMNode->appendChild(Object(DOMElement)) #1 /in/f1lGb(29): EformDOMDocument->buildMissingTree('//bids_offers/m...') #2 {main} thrown in /in/f1lGb on line 87
Process exited with code 255.
Output for 5.0.3 - 5.0.4
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in /in/f1lGb:86 Stack trace: #0 /in/f1lGb(86): DOMDocument->appendChild(Object(DOMElement)) #1 /in/f1lGb(28): EformDOMDocument->buildMissingTree('//bids_offers/m...') #2 {main} thrown in /in/f1lGb on line 86
Process exited with code 255.
Output for 5.0.1 - 5.0.2
Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in /in/f1lGb:86 Stack trace: #0 /in/f1lGb(28): EformDOMDocument->buildMissingTree(Object(DOMElement)) #1 {main} thrown in /in/f1lGb on line 86
Process exited with code 255.
Output for 4.4.3 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/f1lGb on line 35
Process exited with code 255.
Output for 4.4.2
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/f1lGb on line 36
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/f1lGb on line 35
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/f1lGb on line 35
Process exited with code 255.