/** * Adds an child element to the parent * @param string The name element * @param domNode * @return domNode */ private function addElement($name, $parent = false, $ns = false) { if ($ns) { $el = $this->doc->createElementNS($ns, $name); } else { $el = $this->doc->createElement($name); } if ($parent) { $parent->appendChild($el); } return $el; }
<?php $document = new domDocument("1.0", "utf-8"); $domimp = new domImplementation(); $doctype = $domimp->createDocumentType('asortiment[<!ENTITY y1 "2009"> <!ENTITY y2 "2010">]'); $document->appendChild($doctype); $asortiment = $document->createElementNS("http://mysite.ru", "asort:asortiment"); $document->appendChild($asortiment); $telefon = $document->createElement("asort:telefon"); $asortiment->appendChild($telefon); $attr = $document->createAttribute("number"); $attr->nodeValue = 1; $telefon->appendChild($attr); $cdata = $document->createCDATASection("Это контент нашего интернет магазина"); $telefon->appendChild($cdata); $model_1 = $document->createElement("model"); $telefon->appendChild($model_1); $name_1 = $document->createElement("name"); $model_1->appendChild($name_1); $textN_1 = $document->createTextNode("Samsung Galaxi"); $name_1->appendChild($textN_1); $year_1 = $document->createElement("year"); $model_1->appendChild($year_1); $textY_1 = $document->createTextNode('2009'); $year_1->appendChild($textY_1); $model_2 = $document->createElement("model"); $telefon->appendChild($model_2); $name_2 = $document->createElement("name"); $model_2->appendChild($name_2); $textN_2 = $document->createTextNode("Nokia Lumia"); $name_2->appendChild($textN_2);
try { $dom = new domDocument(); $dom->createElementNS('http://www.w3.org/2000/xmlns/', 'xmlns:valid'); print "valid\n"; } catch (Exception $e) { print $e->getMessage() . "\n"; } print "33 DOMElement::__construct('xmlns:valid', '', 'http://www.w3.org/2000/xmlns/')\n"; try { $element = new DomElement('xmlns:valid', '', 'http://www.w3.org/2000/xmlns/'); print "valid\n"; } catch (Exception $e) { print $e->getMessage() . "\n"; } /* the namespaceURI is "http://www.w3.org/2000/xmlns/" and neither the qualifiedName nor its prefix is "xmlns". */ print "34 DOMDocument::createElementNS('http://www.w3.org/2000/xmlns/', 'wrongprefix:valid')\n"; try { $dom = new domDocument(); $dom->createElementNS('http://www.w3.org/2000/xmlns/', 'wrongprefix:valid'); print "valid\n"; } catch (Exception $e) { print $e->getMessage() . "\n"; } print "35 DOMElement::__construct('wrongprefix:valid', '', 'http://www.w3.org/2000/xmlns/')\n"; try { $element = new DomElement('wrongprefix:valid', '', 'http://www.w3.org/2000/xmlns/'); print "valid\n"; } catch (Exception $e) { print $e->getMessage() . "\n"; }