简体   繁体   中英

DOMdocument: using nodeValue to change html content produces wrongly formatted html file ( &lt;div&gt; instead of <div>)

I am using jquery to let the user position various divs. Afterwards I'm dumping them to a php POST function via jquery's $.post and save them to an external html file through php. The .php file which should handle the writing uses DOMdocument (which I have never used- I also have no knowledge of any XML conventions, if there are any).

The .php file reads:

<?php
$data = $_POST['data'];
$doc = new DOMDocument();
$doc->loadHTMLFile("startfile.html");
$doc->getElementById('container')->nodeValue = $data;
$doc->saveHTMLFile("endfile.html")
?>

It's really simple, and it should work. Howewer, whereas the endfile.html output should be:

<div id="container">
<div class= "dummydata">
</div>
</div>

it actually outputs

<div id="container">
 &lt;/class= "dummydata"&gt;
 &lt;/div&gt;
</div>

Which of course cannot be correctly parsed by the browser. I am sure I've been missing something simple and I'll feel extremely stupid once the solution is before my eyes. I don't want to resort to fopen/fwrite as this seemed like a very elegant and flexible way of handling html files.

edit: since the suggested XML fragment parsing kept failing due to unproper formatting (probably just cosmeting, seeing as i'm basically passing working HTML code - might it be XML is much more sensible in regard to spacing and characters used?- i went with this very hacky solution:

//write HTML with nodevalue
$doc->getElementById('container')->nodeValue = $data;
$doc->saveHTMLFile("../test.html");
//open file and replace stripped < and >s
$file_path ='../test.html'
$content = file_get_contents($file_path);
$content = str_replace('&lt;', '<', $content);
$content = str_replace('&gt;', '>', $content);
file_put_contents($file_path, $content);

It's not very elegant, but it works. Any thoughts? Do you think such a hacky solution might be justified instead of trying to properly format the HTML i'm passing as XML? And what are some possible pitfalls? I can't think of any, seeing i'm just cloning badly formatted but working HTML. Thanks in advance.

Yep, nodeValue will strip the tags.

You'll want to create a fragment of the data, and then append it .

<?php

$data = '<div class="dummydata"></div>';
$doc = new DOMDocument();
$doc->loadHTML('<!DOCTYPE html><div id="container">contents</div>');

$el = $doc->getElementById('container');

$children = $el->childNodes;
while ($children->length)
    $el->removeChild($children->item(0));

$frag = $el->ownerDocument->createDocumentFragment();
$frag->appendXML($data);
$el->appendChild($frag);

echo $doc->saveHTML();

Be sure to replace the loadHTML and saveHTML with your code if you want to integrate it.

Output :

<!DOCTYPE html>
<html><body><div id="container"><div class="dummydata"></div></div></body></html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM