For exemple, i create a DOMDocument
like that :
<?php
$implementation = new DOMImplementation();
$dtd =
$implementation->createDocumentType
(
'html', // qualifiedName
'-//W3C//DTD XHTML 1.0 Transitional//EN', // publicId
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-'
.'transitional.dtd' // systemId
);
$document = $implementation->createDocument('', '', $dtd);
$elementHtml = $document->createElement('html');
$elementHead = $document->createElement('head');
$elementBody = $document->createElement('body');
$elementTitle = $document->createElement('title');
$textTitre = $document->createTextNode('My bweb page');
$attrLang = $document->createAttribute('lang');
$attrLang->value = 'en';
$document->appendChild($elementHtml);
$elementHtml->appendChild($elementHead);
$elementHtml->appendChild($attrLang);
$elementHead->appendChild($elementTitle);
$elementTitle->appendChild($textTitre);
$elementHtml->appendChild($elementBody);
So, now, if i have some xhtml string like that :
<?php
$xhtml = '<h1>Hello</h1><p>World</p>';
How can i import it in the <body>
node of my DOMDocument
?
For now, the only solution I've found, is something like that :
<?php
$simpleXmlElement = new SimpleXMLElement($xhtml);
$domElement = dom_import_simplexml($simpleXmlElement);
$domElement = $document->importNode($domElement, true);
$elementBody->appendChild($domElement);
This solution seems very bad for me, and create some problemes, like when I try with a string like that :
<?php
$xhtml = '<p>Hello World</p>';
Ok, I can bypass this problem by converting xhtml entities in Unicode entities, but it's so ugly...
Any help ?
Thanks by advance !
Related question :
DOMDocument::validate()
problem (solved)