php.de

Zurück   php.de > php.de Intern > Wiki Diskussionsforum > Tutorials

Tutorials Hier findest Du Tutorials, welche nach und nach ein fertiges Script ergeben. Sehen, lernen & verstehen!

Antwort
 
LinkBack Themen-Optionen Thema bewerten
Alt 27.11.2009, 23:02  
Moderator
 
Benutzerbild von Chriz
 
Registriert seit: 11.05.2008
Beiträge: 6.069
Chriz ist ein wunderbarer AnblickChriz ist ein wunderbarer AnblickChriz ist ein wunderbarer AnblickChriz ist ein wunderbarer AnblickChriz ist ein wunderbarer AnblickChriz ist ein wunderbarer AnblickChriz ist ein wunderbarer Anblick
Standard DOM: No Modification Allowed Error

So, ich bin jetzt schon wieder drüber gestolpert und stand schon wieder wie der Ochs vorm Berg:

Code:
No Modification Allowed Error
#0 E:\Projekte\localhost\dom2.php(38): DOMNode->appendChild(Object(DOMElement))

#1 E:\Projekte\localhost\dom2.php(32): Head->_build(Object(Html))

#2 E:\Projekte\localhost\dom2.php(17): Head->__construct(Object(Html))

#3 E:\Projekte\localhost\dom2.php(54): Html->saveXml()

#4 {main}
PHP-Code:
PHP-Code:
<?php
class Html extends DOMDocument
{
    protected 
$_built false;
    
    public function 
__construct()
    {
        
parent::__construct();
        
        
$elem $this->createElement("html");
        
$this->appendChild($elem);
    }
    
    public function 
saveXml()
    {
        if (!
$this->_built) {
            
$head = new Head($this);
            
$this->documentElement->appendChild($head);
            
            
$body = new Body($this);
            
$this->documentElement->appendChild($body);
        }
        return 
parent::saveXml();
    }
}

class 
Head extends DOMElement
{
    public function 
__construct(DOMDocument $doc)
    {
        
parent::__construct("head");
        
$this->_build($doc);
    }
    
    protected function 
_build(DOMDocument $doc)
    {
        
$elem $doc->createElement("title""My Title");
        
$this->appendChild($elem);
    }
}

class 
Body extends DOMElement
{
    public function 
__construct()
    {
        
parent::__construct("body");
        
        
// ..
    
}
}

try {
    
$html = new Html();
    echo 
"<pre>"htmlentities($html->saveXml()), "</pre>";
} catch (
Exception $e) {
    echo 
"<h1>"$e->getMessage(), "</h1>";
    echo 
nl2br($e->getTraceAsString());
}
Der Fehler ist einfach der, dass ein DOMElement nicht modifiziert werden kann, bevor es nicht in ein DOMDocument eingehängt wurde. Mehr Spuk ist es nicht. Man braucht auch kein registerNodeClass() oder solche Späße.

Hier der funktionierende Code:
PHP-Code:
<?php
class Html extends DOMDocument
{
    protected 
$_built false;
    
    public function 
__construct()
    {
        
parent::__construct();
        
        
$elem $this->createElement("html");
        
$this->appendChild($elem);
    }
    
    public function 
saveXml()
    {
        if (!
$this->_built) {
            
$head $this->documentElement->appendChild(new Head()); // append first
            
$head->build(); // then modify
            
            
$body $this->documentElement->appendChild(new Body());
            
$body->build();
        }
        return 
parent::saveXml();
    }
}

class 
Head extends DOMElement
{
    public function 
__construct()
    {
        
parent::__construct("head");
    }
    
    public function 
build()
    {
        
$elem $this->ownerDocument->createElement("title""My Title");
        
$this->appendChild($elem);
    }
}

class 
Body extends DOMElement
{
    public function 
__construct()
    {
        
parent::__construct("body");
    }
    
    public function 
build()
    {
        
$elem $this->ownerDocument->createElement("h1""It's working");
        
$this->appendChild($elem);
    }
}

try {
    
$html = new Html();
    echo 
"<pre>"htmlentities($html->saveXml()), "</pre>";
} catch (
Exception $e) {
    echo 
"<h1>"$e->getMessage(), "</h1>";
    echo 
nl2br($e->getTraceAsString());
}
?>
Ausgabe:
Code:
<?xml version="1.0"?>
<html><head><title>My Title</title></head><body><h1>It's working</h1></body></html>
Die XML-Deklaration (wegzubekommen) ist dabei jetzt zweitrangig.

Siehe auch Handbuch:
DOMDocument:
PHP: DOMDocument - Manual

DOMDocument->documentElement:
PHP: DOMDocument - Manual

DOMNode->ownerDocument:
PHP: DOMNode - Manual

</Interne Notiz an mich, Ende>
Chriz ist offline   Mit Zitat antworten
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Antwort


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
[Erledigt] Verarbeiten eines Fatal Error starwhooper PHP Tipps 2009 10 12.11.2009 14:54
Fehler mysql error number: 1064 slypher01 Trash 6 20.10.2009 21:44
[Erledigt] Joomla php fatal error EinNickname PHP Tipps 2009 7 13.07.2009 11:32
Parse error: syntax error, unexpected T_VARIABLE maternus PHP Tipps 2009 23 11.07.2009 17:58
Error: Parse error: syntax error, unexpected $end Knuff Trash 21 29.01.2009 16:14
[Erledigt] Parse error: syntax error, unexpected T_IF Volker59 PHP Tipps 2008 5 01.11.2008 20:14
Parse error: syntax error, unexpected T_BOOLEAN_AND Saschilys PHP Tipps 2008 7 22.05.2008 16:14
Spionageversuch? Code-Injection? saibot PHP-Fortgeschrittene 24 06.10.2007 02:07
xmlrpc-epi kompilieren [gelöst] freq.9 Server, Hosting und Workstations 4 24.07.2005 21:45
[Erledigt] Fatal error: Allowed memory size... PHP Tipps 2005-2 5 07.07.2005 13:01
Parse error: parse error, unexpected $... GrU3nL!nG PHP Tipps 2005-2 4 11.06.2005 18:10
Fatal error: Allowed memory size of 8192 bytes exhausted at PHP Tipps 2005 11 06.03.2005 19:26
Internal Server Error Skazi PHP Tipps 2004 2 06.10.2004 22:31
Fatal error: Allowed memory size of PHP Tipps 2004 4 04.09.2004 17:50
Parse error: parse error, unexpected $ Hilfe!!! PHP Tipps 2004 8 29.06.2004 21:51

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
no modification allowed error, domexception: no modification allowed error, \'domexception\' with message \'no modification allowed error\', domelement no modification allowed error, \no modification allowed error\, php no modification allowed error, no modification allowed error dom, bbcode allowed, no modification allowed error php, domexception no modification allowed error, \'no modification allowed error\', dom no modification allowed error, parse error: syntax error, unexpected t_variable $root = $doc->createelement(\'html\');, php domelement no modification allowed error, no modification allowed error setattribute, php domdocument no modification allowed error, php dom no modification allowed, domexception\' with message \'no modification allowed error\', \'no modification allowed error, domexception: no modification allowed error setattribute

Alle Zeitangaben in WEZ +1. Es ist jetzt 09:55 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum